On the "SSH Advanced path" hibernate mapping-a one-to-many correlation mapping (vii), we present a pair of multi-correlation mappings, which is the basis of many-to-many association mappings.
Many-to-many mappings are the most common mapping in real life and the easiest to understand. Talk less and start straight.
Mapping principle
Whether a one-way or two-way association is through the third table, the primary key in two tables is placed into the third one to make an association. Use the third table to solve problems that may result in data redundancy.
Example
One user to multiple roles (role), one role to multiple users.
Classification
One-way, many-to-many association mappings (one-way user--->role)
Object model
Relational model
Instance
Let's look at the code for the entity class and the mapping file.
User
Package Com.liang.hibernate;import Java.util.set;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 Set GetRoles () {return roles;} public void Setroles (Set roles) {this.roles = roles;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Role
Package Com.liang.hibernate;public class Role {private int id;private String name;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;}}
User.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Role.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure and test data
Many-to-many association mappings, in entity classes, are represented by a set, as with a one-to-many association map. <set> tags rename the middle table name with the Table property,<key> the label defines the primary key for the current table, and a <many-to-many> tag to associate another table.
Bidirectional multi-to-many correlation mapping (bidirectional user<--->role)Object model
Relational model
Ditto
Instance
Let's look at the code for the entity class and the mapping file.
User
Package Com.liang.hibernate;import Java.util.set;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 Set GetRoles () {return roles;} public void Setroles (Set roles) {this.roles = roles;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Role
Package Com.liang.hibernate;import Java.util.set;public class Role {private int id;private String name;private Set users; Public Set Getusers () {return users;} public void Setusers (Set users) {this.users = 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;}}
User.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Role.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
The resulting table structure and test data
In many-to-many bidirectional relationships, the mapping file for user and role is the same, and it's worth noting that the generated intermediate table names must be the same, and the fields that generate the intermediate tables must be the same.
SummarizeMany-to-many association mappings come to an end, with the learning of a one-to-many association mapping, Relatively speaking , many-to-many association mappings become very simple, much like a variant of a one-to-many association map.
Hibernate's summary is immediately killed, thank you for your attention.
"SSH Advanced path" hibernate mapping--many-to-many association mappings (eight)