Many-to-many relationships: one row in the first table can be related to one or more rows in the second table. One row in the second table can also be related to one or more rows in the first table.
In general design, many-to-many association mappings require an intermediate table hibernate uses Many-to-many tags to represent many-to-many association mappings, in entity classes, like a pair of many, are represented by a set
1. Entity
1.1 Course Entities
Packagedemo.entity;ImportJava.util.HashSet;ImportJava.util.Set;/*** Course Entity *@authorDon * @date: Date: April 11, 2017 time: 2:25:45* *@version1.0*/ Public classCourse {PrivateString ID; PrivateString name; //List of students included in the course PrivateSet<student> Stus =NewHashset<>(); PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicSet<student>Getstus () {returnStus; } Public voidSetstus (set<student>stus) { This. Stus =Stus; } PublicCourse () {} PublicCourse (String name) { This. SetName (name); }}
View Code
1.2 Student Entities
Packagedemo.entity;ImportJava.util.HashSet;ImportJava.util.Set;/*** Student Entity *@authorDon * @date: Date: April 11, 2017 time: 2:25:15* *@version1.0*/ Public classStudent {PrivateString ID; PrivateString name; //List of courses selected by students PrivateSet<course> cous=NewHashset<>(); PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicSet<course>getcous () {returncous; } Public voidSetcous (set<course>cous) { This. cous =cous; } PublicStudent () {} PublicStudent (String name) { This. SetName (name); }}
View Code
2. Relational mapping configuration
2.1 Course Entity Mapping
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourcefo Rge.net/hibernate-mapping-3.0.dtd "><hibernate-mapping> <!--Name: Entity, table: List name - <classname= "Demo.entity.Course"Table= "M2m_1_course"> <!--Name : Primary key, column: Primary key database table column, identity self-increment - <IDname= "Id"> <!--Generate primary Key - <Generatorclass= "UUID"></Generator> </ID> <!--Name: attribute names in entities, Length: Lengths, Column: Fields in table (Entity attributes and fields can be omitted), type: types (can not be written automatically by Hiberbate) - < Propertyname= "Name" /> <!--building a collection with data from an intermediate table - <Setname= "Stus"Table= "M2m_1_stu_cour"Cascade= "Save-update"> <!--column in the middle table that records the current class - <Keycolumn= "CID"></Key> <Many-to-manyclass= "Demo.entity.Student"column= "Stuid"></Many-to-many> </Set> </class></hibernate-mapping>
2.2 Student Entity Mapping
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourcefo Rge.net/hibernate-mapping-3.0.dtd "><hibernate-mapping> <!--in this one-to-one relationship, the person can generate the primary key value himself - <!--Name: Entity, table: List name - <classname= "Demo.entity.Student"Table= "M2m_1_student"> <!--Name : Primary key, column: Primary key database table column, identity self-increment - <IDname= "Id"> <!--Hibernate uses the Generator class to generate a primary key - <Generatorclass= "UUID" /> </ID> <!--Name: attribute names in entities, Length: Lengths, Column: Fields in table (Entity attributes and fields can be omitted), type: types (can not be written automatically by Hiberbate) - < Propertyname= "Name" /> <!--building a collection with data from an intermediate table - <Setname= "Cous"Table= "M2m_1_stu_cour"Cascade= "Save-update"> <!--column in the middle table that records the current class - <Keycolumn= "Stuid"></Key> <Many-to-manyclass= "Demo.entity.Course"column= "CID"></Many-to-many> </Set> </class></hibernate-mapping>
3. Test Add, delete
Packagedemo.test;Importorg.hibernate.classic.Session;ImportDemo.entity.Course;Importdemo.entity.Student;ImportDemo.util.HibernateUtil; Public classTestsave { Public Static voidMain (string[] args) {Student stu1=NewStudent ("Zhang three"); Student STU2=NewStudent ("John Doe"); Student STU3=NewStudent ("Harry"); Course C1=NewCourse ("C #"); Course C2=NewCourse ("JAVA"); Course C3=NewCourse ("PHP"); Course C4=NewCourse ("Object-c"); //students add elective coursesstu1.getcous (). Add (C1); Stu1.getcous (). Add (C2); Stu2.getcous (). Add (C2); Stu2.getcous (). Add (C4); Stu3.getcous (). Add (C1); Stu3.getcous (). Add (C2); Stu3.getcous (). Add (C3); Stu3.getcous (). Add (C3); Session Session=hibernateutil.getcurrentsession (); Session.begintransaction (); Session.save (STU1); Session.save (STU2); Session.save (STU3); /*Add an elective lesson again*/ //Student RecordsStudent stu = (Student) session.get (Student.class, "2"); //Course HistoryCourse course= (Course) session.get (Course.class, "1"); Stu.getcous (). Add (course); Session.update (Stu); /*Delete Selected Courses*/ //Student RecordsStudent Studelete = (Student) session.get (Student.class, "2"); //Course HistoryCourse coudelete= (Course) session.get (Course.class, "1"); Studelete.getcous (). Remove (Coudelete); Session.update (Studelete); //Delete Student RecordsSession.delete (Studelete); Session.gettransaction (). commit (); }}
View Code
Hibernate multi-table relationship configuration--many-to-many relationship mappings