Multiple-to-multiple examples of hibernate (add images)

Source: Internet
Author: User

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
Class
Teacher {

 Private
Int
ID;

PrivateString
Name;

Set <student> students;

Public
Int
GETID (){

Return
ID;

}

Public
Void
Setid (IntID ){

This. ID = ID;

}

PublicString getname (){

Return
Name;

}

Public
Void
Setname (string name ){

This. Name = Name;

}

PublicSet <student> getstudents (){

Return
Students;

}

Public
Void
Setstudents (set <student> students ){

This. Students = students;

}

 

}

2student class

PackageCom. Fish. testdao;

 

ImportJava. util. Set;

 

Public
Class
Student {

Private
Int
ID;

PrivateString
Name;

Set <teacher> teachers;

Public
Int
GETID (){

Return
ID;

}

Public
Void
Setid (IntID ){

This. ID = ID;

}

PublicString getname (){

Return
Name;

}

Public
Void
Setname (string name ){

This. Name = Name;

}

PublicSet <teacher> getteachers (){

Return
Teachers;

}

Public
Void
Setteachers (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:


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.