There are also many-to-many relationships in hibernate. However, the execution efficiency of this relationship is not high, so we can achieve it through two to one or two to more.
Many-to-many relationships are also common in real life. For example, teachers and students. One teacher has multiple students, and one student has multiple teachers.
We can create an intermediate table for the relationship between teachers and students. The role of the intermediate table is to associate teachers with students.
Please refer to this table:
This is the relationship between the three tables. Now I will write an instance to implement the above relationship.
1teacher class
PackageCom. Fish. testdao;
ImportJava. util. Set;
Public
ClassTeacher {
Private
IntID;
PrivateString
Name;
Set <student> students;
Public
IntGETID (){
Return
ID;
}
Public
VoidSetid (IntID ){
This. ID = ID;
}
PublicString getname (){
Return
Name;
}
Public
VoidSetname (string name ){
This. Name = Name;
}
PublicSet <student> getstudents (){
Return
Students;
}
Public
VoidSetstudents (set <student> students ){
This. Students = students;
}
}
2student class
PackageCom. Fish. testdao;
ImportJava. util. Set;
Public
ClassStudent {
Private
IntID;
PrivateString
Name;
Set <teacher> teachers;
Public
IntGETID (){
Return
ID;
}
Public
VoidSetid (IntID ){
This. ID = ID;
}
PublicString getname (){
Return
Name;
}
Public
VoidSetname (string name ){
This. Name = Name;
}
PublicSet <teacher> getteachers (){
Return
Teachers;
}
Public
VoidSetteachers (set <teacher> teachers ){
This. Teachers = teachers;
}
}
3. Student XML
<? XML
Version = "1.0"
Encoding = "UTF-8"?>
<! Doctype
Hibernate-mapping public
"-// Hibernate/hibernatemapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class
Name = "com. Fish. testdao. Student">
<ID
Name = "ID"
Type = "integer">
<Generator
Class = "increment"> </generator>
</ID>
<Property
Name = "name"> </property>
<Set
Name = "Teachers"
Table = "teacher_student"> // We will generate a teacher-student association table.
<Key
Column = "student_id"> </key> // then, a student_id field is generated in this table.
<Role-to-Role
Class = "com. Fish. testdao. Teacher" column = "teacher_id"> </role-to-operate>
</Set>
</Class>
</Hibernate-mapping>
* Note: set is used to associate intermediate tables. In fact, this can be used in a table. This set can be omitted in the XML of techer. Of course, there is no mistake in writing.
4. xml of techer
<? XML
Version = "1.0"
Encoding = "UTF-8"?>
<! Doctype
Hibernate-mapping public
"-// Hibernate/hibernatemapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping>
<Class
Name = "com. Fish. testdao. Teacher">
<ID
Name = "ID"
Type = "integer">
<Generator
Class = "increment"> </generator>
</ID>
<Property
Name = "name"> </property>
<Set
Name = "Students"
Table = "teacher_student">
<Key
Column = "teacher_id"> </key>
<Role-to-Role
Class = "com. Fish. testdao. Student" column = "student_id"
/>
</Set>
</Class>
</Hibernate-mapping>
5. Let's write a test method.
Packagecom. Fish. domain;
Importjava. util. hashset;
Import java. util. List;
Import java. util. Set;
Importorg. hibernate. query;
Importorg. hibernate. Session;
Importorg. hibernate. sessionfactory;
Importorg. hibernate. transaction;
Import org. hibernate. cfg. configuration;
Importcom. Fish. testdao. student;
Importcom. Fish. testdao. teacher;
Public class test4 {
Public static session getmysession (){
Configuration configuration = new configuration ();
Configuration. Configure ("hibernate. cfg. xml ");
Sessionfactory factory = configuration. buildsessionfactory ();
Session session = factory. opensession ();
Return session;
}
// Add data to the three tables. I commented on this method. If you add data to the intermediate table at the same time, an error is returned. Imagine that both sets describe a relationship. Then, an error will be reported when inserting at the same time.
Public static void add (){
// Set <student> setstu = newhashset <student> ();
Set <teacher> settea = newhashset <teacher> ();
Teacher = new teacher ();
Student = new student ();
Student. setname ("qwe ");
Teacher. setname ("Miss Huang ");
// Setstu. Add (student );
Settea. Add (teacher );
Student. setteachers (settea );
// Teacher. setstudents (setstu );
Session session = getmysession ();
Transaction = session. begintransaction ();
Transaction. Begin ();
Session. Save (student );
Session. Save (teacher );
Transaction. Commit ();
Session. Close ();
}
Public static void query (){
String hql = "from Teacher ";
Session session = getmysession ();
Query query = session. createquery (hql );
List <teacher> List = query. List ();
For (teacher I: List ){
System. Out. println (I. getname () + "Students :");
For (student J: I. getstudents ()){
System. Out. Print (J. getname ());
}
System. Out. println ();
}
Session. Close ();
}
Public static void main (string [] ARGs ){
Query ();
}
}
Let me show you the data in the databases of the three tables.
The query result is: