1-One-to-many mappings (One-to-many)
@Entity @Table (name= "T_classroom") public class Classroom {private int id;
Private String ClassName;
Private set<student> students; The @OneToMany (mappedby= "room")--->onetomany specifies a one-to-many relationship, and mappedby= "room" specifies the number of parties to maintain the relationship. Mappedby refers to the dependency properties of one side on the other, (note: If no one is specified to maintain the association, the system creates a middle table) @LazyCollection (Lazycollectionoption.extra)---& Gt
The Lazycollection property is set to extra specifies that if you query the number of data, only one statement of Count (*) will be issued, improving performance public set<student> getstudents () {
return students;
@Entity @Table (name= "t_student") public class Student {private int id;
private String name;
private int age;
Private classroom room; @ManyToOne (fetch=fetchtype.lazy)---> manytoone specifies a many-to-many relationship in which the Fetch=fetchtype.lazy attribute represents the loading of objects by deferred loading (default is not deferred loading @JoinColumn (name= "RID")---> Specifies the name of the foreign key through the Joincolumn Name property (Note: If we do not specify the name of the foreign key through Joincolum, the system will declare a name) p
Ublic Classroom Getroom () {return room; }
}
In determining who is maintaining an association relationship, you can look at the foreign key, which entity class defines the foreign key, and which class is responsible for maintaining the associated relationship.
2. One-to-one mapping (one-to-one)
@Entity
@Table (name= "T_person") Public
class person
{
private int id;
private String name;
Private Idcard card;
The @OneToOne (mappedby= "person")
---> Specifies the Onetoone Association, and Mappedby also specifies that the other will maintain the associated relationship public
Idcard Getcard ()
{return card
;
}
}
@Entity
@Table (name= "T_id_card") Public
class Idcard
{
private int id;
Private String No;
private person person;
@OneToOne
--->onetoone specifies a one-to-one relationship, one at a random party to maintain the mapping relationship, here Select Idcard for maintenance
@JoinColumn (name= "pid")
---> Specify the name of the foreign key PID public person
Getperson () {return person
;
}
}
3.many-to-many Mappings (Many-to-many mapping relationships)
3.1 Maintain the association relationship through the intermediate table by any more than one by one parties
@Entity @Table (name= "T_teacher") public class Teacher {private int id;
private String name;
Private set<course> courses; @ManyToMany (mappedby= "Teachers")---> indicates that the side of the Course is maintaining public set<course> getcourses () {retur
n Courses;
@Entity @Table (name= "T_course") public class Course {private int id;
private String name;
Private set<teacher> teachers; @ManyToMany---> Manytomany Specify Many-to-many Association @JoinTable (name= "T_teacher_course", joincolumns={@JoinColumn (name= "CID")} , the inversejoincolumns={@JoinColumn (name = "Tid")})---> Because a single intermediate table is used to maintain the direct relationship between the two tables, and by jointable this annotation, name is the specified The name of the table, Joincolumns is a @JoinColumn type of array, represents my side in the other side of the foreign key name, we are course, so the name of the other foreign key is the RID, Inversejoincolumns is also a @ Joincolumn type of array, represents the other side in my place in the foreign key name, the other is Teacher, so the name of our foreign key is tid public set<teacher> getteachers () {RET
URN teachers; }
}
3.2 Splits the many-to-many into two one-to-many mappings (Admin, role, Adminrole)
@Entity @Table (name= "T_admin") public class Admin {private int id;
private String name;
Private set<adminrole> ars;
Public Admin () {ars = new hashset<adminrole> ();
} public void Add (Adminrole ar) {ars.add (AR); @OneToMany (mappedby= "admin")--->onetomany is associated with the Adminrole class, which maintains a many-to-many relationship by Adminrole This class, mappedby= "admin" @Laz
Ycollection (Lazycollectionoption.extra) public set<adminrole> Getars () {return ARS;
}} @Entity @Table (name= "T_role") public class role {private int id;
private String name;
Private set<adminrole> ars;
Public role () {ars = new hashset<adminrole> ();
} public void Add (Adminrole ar) {ars.add (AR); @OneToMany (mappedby= "role")--->onetomany specifies that the Adminrole class is to maintain a many-to-many association, mappedby= "role" @LazyCollection (L Azycollectionoption.extra) public set<adminrole> Getars () {RETurn ARS;
@Entity @Table (name= "T_admin_role") public class Adminrole {private int id;
private String name;
Private admin admin;
private role role; @ManyToOne---> manytoone associated to the admin @JoinColumn (name= "aid") public admin getadmin () {return admi
N
@ManyToOne---> @JoinColumn (name= "rid") public role Getrole () {return role; }
}
When inserting by hibernate, whether it's one-to-many, one-to-one, or many-to-many, you just need to remember that the entity class declares the foreign key, which class maintains the relationship.
When you save the data, you always save the data of the party that has not maintained the association, and then save the data for the party that maintains the relationship, such as:
Person p = new person ();
P.setname ("Xiaoluo");
Session.save (p);
Idcard card = new Idcard ();
Card.setno ("1111111111");
Card.setperson (p);
Session.save (card);
The above content is collated from http://www.cnblogs.com/xiaoluo501395377/p/3374955.html, in this thank Bo Lord