Java framework --- hibernate (many-to-many) ing, java --- hibernate

Source: Internet
Author: User

Java framework --- hibernate (many-to-many) ing, java --- hibernate

Take students and teachers as examples to illustrate multi-to-Multi- ing.

Entity class:

Student
package cn.itcast.g_hbm_manyToMany;import java.util.HashSet;import java.util.Set;public class Student {    private Long id;    private String name;    private Set<Teacher> teachers = new HashSet<Teacher>();    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Set<Teacher> getTeachers() {        return teachers;    }    public void setTeachers(Set<Teacher> teachers) {        this.teachers = teachers;    }    @Override    public String toString() {        return "[Student: id=" + id + ", name=" + name + "]";    }}
Teacher 
package cn.itcast.g_hbm_manyToMany;import java.util.HashSet;import java.util.Set;public class Teacher {    private Long id;    private String name;    private Set<Student> students = new HashSet<Student>();         public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Set<Student> getStudents() {        return students;    }    public void setStudents(Set<Student> students) {        this.students = students;    }    @Override    public String toString() {        return "[Teacher: id=" + id + ", name=" + name + "]";    }}

Ing file:

Student.hbm.xml
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
Teacher.hbm.xml
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Test

App.java
Package cn. itcast. g_hbm_manytow.import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. test; import com. java1234.util. hibernateSessionFactory; public class App {private static SessionFactory sessionFactory = HibernateSessionFactory. getSessionFactory (); // save, associated @ Test public void testSave () throws Exception {Session session = ses SionFactory. openSession (); session. beginTransaction (); // create a new object Student student1 = new Student (); student1.setName ("Wang 1"); Student student2 = new Student (); student2.setName (" 2"); Teacher teacher1 = new Teacher (); teacher1.setName (" 3"); Teacher teacsp2 = new Teacher (); teacher2.setName ("instructor Cai 4"); // student1.getTeachers (). add (teacher1); stu Dent1.getTeachers (). add (teachers); student2.getTeachers (). add (teacher1); student2.getTeachers (). add (teacsp2); teacher1.getStudents (). add (student1); teacher1.getStudents (). add (student2); teacher2.getStudents (). add (student1); teacher2.getStudents (). add (student2); // Save the session. save (student1); session. save (student2); session. save (teacher1); session. save (teacher );//-------------------------------- ------------ Session. getTransaction (). commit (); session. close ();} // obtain the associated peer @ Test public void testGet () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // obtain one party and display the other party's information. Teacher teacher = (Teacher) session. get (Teacher. class, 3L); System. out. println (teacher); System. out. println (teacher. getStudents (); // ---------------------------------------------- Session. getTransaction (). commit (); session. close ();} // unassociate @ Test public void testRemoveRelation () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // If inverse = false, the Teacher session cannot be revoked if true. get (Teacher. class, 3L); teac Her. getStudents (). clear (); // ------------------------------------------------ session. getTransaction (). commit (); session. close ();} // Delete the object, impact on the associated object @ Test public void testDelete () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // a. If there is no associated peer, delete it. // B. If there is an associated peer and inverse = false, the associated link is deleted first and then deleted. // C. If there is an associated peer and inverse = true, because the association cannot be maintained, the system will directly delete itself, and an exception will occur. Teacher teacher = (Teacher) session. get (Teacher. class, 9L); session. delete (teacher); // ------------------------------------------------ session. getTransaction (). commit (); session. close ();}}

The many-to-many relationship between two tables is usually achieved through the third intermediate table in the database. The third intermediate table contains the primary key values of the two tables, the ing between the primary key and the primary key reflects the direct relationship between the table. For example, in a permission system, a user can have multiple permissions, and a permission can also be granted to multiple users.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.