首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

心得7-hibernate级联之多对多转换为多对一的有关问题剖析

2012-12-30 
心得7-hibernate级联之多对多转换为多对一的问题剖析这里与多对多不同的是借用了一个中间类StudentCourse

心得7-hibernate级联之多对多转换为多对一的问题剖析

   这里与多对多不同的是借用了一个中间类StudentCourse来达到把多对多的问题转换为多对一的问题,下面来举例具体介绍:   先看看三个最重要的xxx.hbm.xml文件内部信息Student.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hbsi.many2manyTOone2many">     <class name="Student" table="s">        <id name="id">           <generator class="native"/>        </id>        <property name="name" column="name"/>                <set name="sc" table="s_c" inverse="true">          <key column="s_id"/>          <one-to-many class="StudentCourse"/>        </set>    </class>    </hibernate-mapping> Course.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hbsi.many2manyTOone2many">     <class name="Course" table="c">        <id name="id">           <generator class="native"/>        </id>        <property name="name" column="name"/>                <set name="sc" table="s_c" inverse="true">          <key column="c_id"/>          <one-to-many class="StudentCourse"/>        </set>    </class>    </hibernate-mapping> StudentCourse.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hbsi.many2manyTOone2many">     <class name="StudentCourse" table="s_c">        <id name="id">           <generator class="native"/>        </id>        <property name="score" column="score"/>        <many-to-one name="s" column="s_id" cascade="save-update"/><many-to-one name="c" column="c_id" cascade="save-update"/>     </class>    </hibernate-mapping>   再看看测试文件TestApp.javapackage com.hbsi.many2manyTOone2many; import java.util.Set; import org.hibernate.Session;import org.junit.Test; import com.hbsi.utils.HibernateSessionFactory; public class TestApp {      Session session = HibernateSessionFactory.getSession();   @Test   public void add(){     session.beginTransaction();          Student stu = new Student();     stu.setName("keven");          Course c1 = new Course();     c1.setName("java");          Course c2 = new Course();     c2.setName("mysql");          StudentCourse sc1 = new StudentCourse();     sc1.setS(stu);     sc1.setC(c1);     sc1.setScore(99);          session.save(sc1);          StudentCourse sc2 = new StudentCourse();     sc2.setS(stu);     sc2.setC(c2);     sc2.setScore(89);          session.save(sc2);               session.getTransaction().commit();     HibernateSessionFactory.closeSession();        }   @Test   public void find(){      Student stu = (Student) session.get(Student.class,1);      Set<StudentCourse> scs = stu.getSc();      for(StudentCourse sc : scs){        System.out.println(sc.getS().getName()+":"+sc.getC().getName()+"---"+sc.getScore());      }   }}


 

热点排行