hibernate多對多關聯

來源:互聯網
上載者:User

標籤:hibernate

多對多表的搭建

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/83/CF/wKioL1d87xPggvdzAAAwpa3N7NE022.jpg" title="1" alt="wKioL1d87xPggvdzAAAwpa3N7NE022.jpg" />



java類中

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/83/CF/wKioL1d871rTAFW6AAAnotK1jjg172.jpg" title="36020160706194558725.jpg" alt="wKioL1d871rTAFW6AAAnotK1jjg172.jpg" />

多對多

  1、關係操作

       1、多對多,誰操作效率都一樣

       2、解除關係

             把第三張表的一行資料刪除掉

       3、建立關係

             把第三張表的資料增加一行記錄

       4、變更關係

             先刪除後增加

  2、級聯操作

       都是對象針對集合的操作

例子

 

工具類

public class HibernateUtils {    public static SessionFactory sessionFactory;    public static String url;    @Before    public void init(){        Configuration configuration = new Configuration();        if(url==null){            configuration.configure();        }else{            configuration.configure(url);        }        sessionFactory = configuration.buildSessionFactory();}

 

student對應檔

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="cn.itcast.many.Student">        <id name="sid" length="5">            <generator class="increment"></generator>        </id>        <property name="sname" length="20"></property>        <property name="description" length="100"></property>        <set name="coursess" table="student_course" cascade="save-update">        <key>            <column name="sid"></column>        </key>        <many-to-many class="cn.itcast.many.Courses" column="cid"></many-to-many>        </set>         </class></hibernate-mapping>

 

courses對應檔

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="cn.itcast.many.Courses">        <id name="cid" length="5">            <generator class="increment"></generator>        </id>        <property name="cname" length="20"></property>        <property name="description" length="100"></property>        <set name="students" table="student_course" cascade="save-update">        <key>            <column name="cid"></column>        </key>         <many-to-many class="cn.itcast.many.Student" column="sid"></many-to-many>        </set>         </class></hibernate-mapping>

 

測試

@Testpublic void testCreateTable(){}@Testpublic void testSaveStudent_Cascade_Courses_Save(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Student student = new Student();    student.setSname("班長");    student.setDescription("牛人");    Courses Courses = new Courses();    Courses.setCname("生理衛生");    Courses.setDescription("很好");    Set<Courses> Coursess = new HashSet<Courses>();    Coursess.add(Courses);    student.setCoursess(Coursess);    session.save(student);    transaction.commit();    session.close();}/** * 已經存在一個課程,建立一個學生,建立課程和學生之間的關係 *    從課程角度出發 */@Testpublic void testUpdateCourses_Cascade_Student_Save_R(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Courses Courses = (Courses)session.get(Courses.class, 1L);    Student student = new Student();    student.setSname("班迷");    student.setDescription("班絲:班長的鋼絲");    Courses.getStudents().add(student);    session.save(student);    transaction.commit();    session.close();}/** * 已經存在一個課程,建立一個學生,建立課程和學生之間的關係 *   從學生角度出發 */@Testpublic void testSaveStudent_R(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Courses Courses = (Courses)session.get(Courses.class, 1L);    Student student = new Student();    student.setSname("班迷");    student.setDescription("班絲:班長的鋼絲");    Set<Courses> Coursess = new HashSet<Courses>();    Coursess.add(Courses);    student.setCoursess(Coursess);    session.save(student);    transaction.commit();    session.close();}/** * 已經存在一個課程,已經存在一個學生,建立關聯 */@Testpublic void testR(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Courses Courses = (Courses)session.get(Courses.class, 1L);    Student student = (Student)session.get(Student.class, 2L);    student.getCoursess().add(Courses);    transaction.commit();    session.close();}/** * 把學生3,4加入到課程1中 */@Testpublic void testR_Some(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Courses Courses = (Courses)session.get(Courses.class, 1L);    Student student = (Student)session.get(Student.class, 3L);    Student student2 = (Student)session.get(Student.class, 4L);    //student.getCoursess().add(Courses);    //student2.getCoursess().add(Courses);    Courses.getStudents().add(student2);    Courses.getStudents().add(student);    transaction.commit();    session.close();}/** * 把一個學生加入到一些課程中 */@Testpublic void testR_Some_2(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Student student = (Student)session.get(Student.class, 3L);    List<Courses> CoursesList = session.createQuery("from Courses where cid in(1,2,3)").list();    student.getCoursess().addAll(CoursesList);    transaction.commit();    session.close();}/** * 把學生從一個課程轉移到另外一個課程 */@Testpublic void testTransform(){    Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    Student student = (Student) session.get(Student.class, 3L);    Courses Courses1 = (Courses)session.get(Courses.class, 1L);    Courses Courses3 = (Courses)session.get(Courses.class, 3L);    student.getCoursess().remove(Courses1);    student.getCoursess().add(Courses3);    transaction.commit();    session.close();}

 

關聯刪除的錯誤

1、根據對應檔可以得出classes與student有關聯

2、在用戶端,students是由classes產生的,代表了關聯關係

3、所以在刪除student的時候,必須通過classes解除關係

4、解除關係以後才能進行刪除


hibernate多對多關聯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.