Hibernate Reading Notes ----- N-N Association ing of Hibernate Association ing.

Source: Internet
Author: User

4. N-N Association
1.1 Association of unidirectional N-N
One-way N-N Association and 1-n association of the persistence class is exactly the same, the end of the control relationship needs to add a set attribute, the associated persistence instance in the form of a set.
N-N associations must use a connection table, and N-N associations are very similar to 1-N associations with a connection table, just remove the unique = "true" for the <strong-to-be-joined.../> element. Other configurations are the same as those associated with 1-N.
Since the association with 1-N is very similar, we will not demonstrate it here.
 
1.2 Association of two-way N-N
For bidirectional N-N associations, we only need to convert to two 1-N Association models. Both ends of a bidirectional N-N association need to use the set property, both ends add access to the set property. A bidirectional N-N must also use a join table to establish associations between two entities.
Take students and teachers as an example: the persistence classes of the two entities are as follows:
Student
[Java]
Public class Student {
Private Integer id;
Private String name;
Private Set <Teacher> teachers;
 
Public Integer getId (){
Return id;
}
 
Public void setId (Integer 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;
}
}
 
Teacher
[Java]
Public class Teacher {
Private Integer id;
Private String name;
Private Set <Student> students;
 
Public Integer getId (){
Return id;
}
 
Public void setId (Integer 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;
}
 
}
 
A ing file associated with a bidirectional N-N needs to use the <set.../> element to map collection properties. <Set... /> you need to add <key... /> the sub-element is always mapped to the foreign key column, and <allow-to-operate... /> child meta maps to associated object classes. The two ing files are as follows:
Student. hbm. xml
[Html]
<Hibernate-mapping package = "com. hibernate. domain">
<Class name = "Student" table = "student">
<Id name = "id" column = "student_id">
<Generator class = "native"/>
</Id>

<Property name = "name" column = "student_name"/>

<! -- Ing the N-N to associate the object, the table on both sides should be sample -->
<Set name = "teachers" table = "student_teacher">
<! -- Map the associated foreign key columns -->
<Key column = "student_id"/>
<! -- Ing Association class attributes -->
<Role-to-define class = "Teacher" column = "teacher_id"/>
</Set>
</Class>
</Hibernate-mapping>

Teacher. hbm. xml
[Html] view plaincopyprint?
<Hibernate-mapping package = "com. hibernate. domain">
<Class name = "Teacher" table = "teacher">
<Id name = "id" column = "teacher_id">
<Generator class = "native"/>
</Id>

<Property name = "name" column = "teacher_name"/>

<! -- Ing the N-N to associate the object, the table on both sides should be sample -->
<Set name = "students" table = "student_teacher">
<! -- Map the associated foreign key columns -->
<Key column = "teacher_id"/>
<! -- Ing Association class attributes -->
<Role-to-train class = "Student" column = "student_id"> </role-to-train>
</Set>
</Class>
</Hibernate-mapping>

Two-way N-N associations both need to specify the name of the joined table, the name of the column of the foreign key column, so the value of the table attribute of the two <set.../> elements must be specified and the same. The column attribute must be specified for the <set.../> element and <key.../> and <sequence-to-sequence.../> child elements. <Key.../> and <failed-to-replicate.../> specify the foreign key columns of the persistence class and association class in the connection table respectively. Therefore, the values of the column attributes of <key.../> and <relative-to-attributes.../> on both sides should be cross-equal.
The following operation class is used to add two Student objects and two Teacher objects.
[Java]
Static void add (){
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();

Teacher teacher1 = new Teacher ();
Teacher1.setName ("teacher1 ");

Teacher = new Teacher ();
Teacher2.setName ("teacher ");

Student student1 = new Student ();
Student1.setName ("student1 ");

Student student2 = new Student ();
Student2.setName ("student2 ");

// Establish the relationship between the two
Set <Student> ss = new HashSet <Student> ();
Ss. add (student1 );
Ss. add (student2 );

Teacher1.setStudents (ss );
Teacher2.setStudents (ss );

Session. save (teacher1 );
Session. save (teacher2 );
Session. save (student1 );
Session. save (student2 );

Tx. commit ();
Session. close ();
}

Note: Only one link can be established here. That is to say, only Student objects can be associated with Teacher objects or Teachert objects can be associated with Student objects. Otherwise, duplicate primary key errors will occur.
Author: chenssy

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.