心得7-hibernate級聯之多對多轉換為多對一的問題剖析

來源:互聯網
上載者:User
   這裡與多對多不同的是借用了一個中間類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());      }   }}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.