the relational mapping of hibernate annotations is divided into 2 kinds, namely one-way association map and two-way association map, and their biggest difference is that one-way association can only be queried while querying the data, and both sides of the two-way association are queried.
a one-way association is a comment that is only added to a party
Two-way association refers to both sides add annotations. Here the main record bidirectional Association.
1. One-to-one correlation:
if there are 2 tables, citizen form person and ID card form Idcard
Party (idcard):
@OneToOne (fetch=fetchtype.lazy,//lazy load
targetentity=idcards.class,//target Object
mappedby= "Person",//specifies which side maintains the relationship (in this case the person table ismaintained, ' person ' refers to the person object associated with the idcards tag )
Cascade=cascadetype.all)
The other party (person):
@OneToOne (Fetch=fetchtype.lazy,
Targetentity=person.class)//Maintain relational tables
@JoinColumn (name= "PersonId", referencedcolumnname= "PersonId", Unique=true)
Specific as follows:
@Entity
@Table (name = "Tb_person") public
class person implements Serializable {
private static final long SE Rialversionuid = 576770011136181361L;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long personId;
@OneToOne (fetch=fetchtype.lazy,targetentity=idcards.class,mappedby= "person", Cascade=cascadetype.all)
Private Idcard Idcard;
@Entity
@Table (name = "Tb_idcard") Public
class Idcard implements Serializable {
private static final long Serialversionuid = 576770011136181369L;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long idcardid;
@OneToOne (Fetch=fetchtype.lazy,
targetentity=person.class)
@JoinColumn (name= "PersonId", Referencedcolumnname= "PersonId", Unique=true)
private person person;
2. One-to-many relationships:
Party:
@OneToMany (Fetch=fetchtype.lazy,
Targetentity=user.class,
Cascade=cascadetype.all,
orphanremoval=true,//Orphan removal
mappedby= "department")
Cascade=cascadetype.all, which is controlled by more than one party;
Multi -:
@ManyToOne (fetch = Fetchtype.lazy,targetentity = Department.class) @JoinColumn (name = "DepartmentID", Referencedcolumnname= "DepartmentID")
Such as: A user can only belong to a department, a department can have a lot of users, the comments are as follows,
Department entity class:
@Entity
@Table (name = "Tb_department") Public
class Department implements Serializable {
Priva Te static final long serialversionuid = -3489189232910430049l;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long DepartmentID;
private String name;
Private String description;
@OneToMany (fetch=fetchtype.lazy,targetentity=user.class,cascade=cascadetype.all,orphanremoval=true,mappedby= " Department ")
private set<user> users;
........
}
User entity class:
@Entity
@Table (name= "Tb_user") public
class user implements Serializable {
private static FINA L Long serialversionuid = 576770011136181361L;
@Id
@GeneratedValue (strategy=generationtype.auto)
private Long userId; @ManyToOne (fetch = Fetchtype.lazy,targetentity = Department.class)
@JoinColumn (name = "DepartmentID", Referencedcolumnname= "DepartmentID")
private Department Department;
Private String loginName;
........
........
}
2. Many-to-many relationships:
Multi -party:
@ManyToMany (Fetch=fetchtype.lazy,targetentity=role.class) @JoinTable (name = "Tb_user_role", Joincolumns = @ Joincolumn (name = "UserID", Referencedcolumnname= "userid"), Inversejoincolumns = @JoinColumn (name = "Roleid", Referencedcolumnname= "Roleid"))
where "tb_user_role" is the relational table of the two, consisting of the primary key ID of both. Specify the name of the field in the relationship table by @JoinColumn (name = "UserId") .
Another multi-party:
@ManyToMany (fetch = fetchtype.lazy, targetentity = user.class, cascade = cascadetype.all, Mappedby = "Roles")
Joincolumns: Refers to the main table, Inversejoincolumns: refers to the matching table
That is User-->role, the user is the primary table and role is the matching table
Such as: A user can have multiple roles, one role can be applied to multiple users.
User entity class:
@Entity
@Table (name= "Tb_user") public
class user implements Serializable {
private static Final long serialversionuid = 576770011136181361L;
@Id
@GeneratedValue (strategy=generationtype.auto)
private Long userId;
Private String loginName;
private String name;
private String gender;
Private String phone;
private String Email;
Private String description;
@ManyToMany (Fetch=fetchtype.lazy,targetentity=role.class)
@JoinTable (name = "Tb_user_role", Joincolumns = @JoinColumn (name = "UserID", Referencedcolumnname= "userid"),
Inversejoincolumns = @JoinColumn (name = "Roleid", Referencedcolumnname= "Roleid"))
private set<role> roles = New Hashset<role> ();
.....
}
Role entity class:
@Entity
@Table (name = "Tb_role") Public
class role implements Serializable {
private static Final long serialversionuid = -7625883839295666830l;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long Roleid;
private String name;
Private String description;
@ManyToMany (fetch = fetchtype.lazy, targetentity = user.class, cascade = cascadetype.all, Mappedby = "roles")
privat E set<user> users = new hashset<user> ();
.....
.....
}
2. Self-linking relationship:
self-connecting party:
@ManyToOne (fetch = fetchtype.lazy, targetentity = Department.class)
@JoinColumn (name = "ParentID", Referencedcolumnname= "DepartmentID")
Self-connected multiparty:
@OneToMany (Fetch=fetchtype.lazy,targetentity=department.class,cascade=cascadetype.all,orphanremoval=true, mappedby= "Parent")
@OrderBy ("DepartmentID ASC")
Because it is self-connected, in a table operation, the user establishes a connection by adding a parent class ID to the tables.
such as: Department management, a department can have a lot of subordinate departments, but a superior department can only have a parent department, then need to establish self-connection.
Department entity class::
@Entity
@Table (name = "Tb_department") Public
class Department implements Serializable {
Private static final long serialversionuid = -3489189232910430049l;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long DepartmentID;
private String name;
Private String description;
@ManyToOne (fetch = fetchtype.lazy, targetentity = department.class)
@JoinColumn (name = "ParentID", Referencedcolumnname= "DepartmentID")
private Department parent;
@OneToMany (Fetch=fetchtype.lazy,targetentity=department.class,cascade=cascadetype.all,orphanremoval=true, mappedby= "parent")
@OrderBy ("DepartmentID ASC")
private set<department> children = new hashset< Department> ();
....
.....
}