Many-to-many bidirectional association mappings
In accordance with our previous practice, take a look at the relevant class diagram and code, as follows:
[Java] View Plain copy print? public class role { private int id; private String name; private Set users; public int getid () { return id; } public void setid ( Int id) { this.id = id; } public string getname () { return name; } public void setname (string name) { this.name = name; } public set getusers () { return users; } public void setusers (set users) { this.users = users; } } public class user { private int id; private string name; private Set roles; public int getid () { return id; } public void setid (int id) { this.id = id; } Public string getname () { return name; } public void SetName (string name) { this.name = name; } public Set GetRoles () { return roles; } public void setroles (Set roles) { this.roles = roles; } }
public class Role {
private int id;
private String name;
Private Set users;
public int getId () {
return ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public Set getusers () {
return users;
}
public void Setusers (Set users) {
this.users = users;
}
}
public class User {
private int id;
private String name;
private Set roles;
public int getId () {
return ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public Set GetRoles () {
return roles;
}
public void Setroles (Set roles) {
this.roles = roles;
}
}
Next we compare the previous post hibernate from Getting started to mastering (10) Many-to-many one-way association mappings in multiple-to-many one-way association mappings, to analyze the many-to-many bidirectional association mappings, and to focus on the difference between the two and the relationship.
similarities and differences of many-to-many bidirectional and many-to-many one-way association mappingsAnalyzing our class diagram and code above, we can see that: looking at one end of many-to-many bidirectional associations alone, there is no fundamental difference between many-to-many bidirectional and many-to-many unidirectional associations. There is a set (set) on each of the many-to-many ends, which is used to store multiple objects associated with it. (Refer to Hibernate from Getting started to mastering (10) Many-to-many one-way association mappings)
Next we look at the difference between the two from the storage structure, as follows:
As can be seen from the above figure, there is no difference between many-to-many unidirectional and bidirectional storage structures. Next, let's look at the configuration information. Specific as follows:
[HTML] View Plain copy print? <class name= "Com.zs.hibernate.Role" table= "T_role" > <id name= "id" > <generator class= " Native "/> </id> <property name= "name"/> <set name= "users" table= "T_user_role" > <key column= "role_id" not-null= " True "/> <many-to-many class=" Com.zs.hibernate.User " column=" user_id "/> </set> </class> <class name= "Com.zs.hibernate.User" table= "T_user" > <id name= "id" > & Nbsp; <generator class= "native"/> </id> <property name= "Name"/> <set Name= "Roles" table= "T_user_role" > <key column= "user_id" not-null= "true"/> <many-to-many class= "Com.zs.hibernate.Role" column= "role_id" /> </set> </class>
<class name= "Com.zs.hibernate.Role" table= "T_role" >
<id name= "id" >
<generator class= "native "/>
</id>
<property name=" name "/>
<set name=" users "table=" T_user_role ">
<key column= "role_id" not-null= "true"/>
<many-to-many class= "Com.zs.hibernate.User" column= "user_id"/ >
</set>
</class>
<class name= "Com.zs.hibernate.User" table= "T_user" >
<id name= "id" >
<generator class= "native"/>
</id>
<property name= "name"/>
<set name= "Roles" table= "T_user_role" >
<key column= "user_id" not-null= "true"/>
< Many-to-many class= "Com.zs.hibernate.Role" column= "role_id"/>
</set>
</class>
Compare our previous posts hibernate from Getting started to mastering (10) The configuration information of many-to-many-way mappings in many-to-many one-way association mappings, we can see that in one-way mapping, can only a->b, not b->a, so a and B relationship is unequal. In the bidirectional correlation mapping, both a->b and b->a, so the relationship between a and B is equivalent. This also causes the configuration information in the two-way association map configuration file to be basically the same for the two classes, namely a=b.