Today, there are many-to-many correlation mappings in Hibernate, and many-to-many correlation mappings involve one-way mapping and two-way mapping of 2.
Let's start with a many-to-many correlation example: User and role roles, one user can belong to multiple roles, and one role can have multiple users. This is a typical example of many-to-many associations. The one-way association mapping is only by the A-terminal to operate the B-terminal, B-terminal can not operate the data at the end. The bidirectional correlation mapping is the data at the other end that can be manipulated at both ends of a and b.
First, one-way association mappings, the entity classes are as follows:
- <span style="font-size:18px" >/**
- * Student Class
- * @author Longxuan
- *
- */
- Public class User {
- private int id;
- private String name;
- private set<role> roles;
- //Omit get and set methods here
- }
- /**
- * Class Category
- * @author Longxuan
- *
- */
- Public class Role {
- private int id;
- private String name;
- //Omit get and set methods here
- }
- </span>
Map file:
- <span style="font-size:18px"><? XML version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="Com.bjpowernode.hibernate">
- <class name="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"></key>
- <many-to-many class="Role" column="Roleid"></many-to-many>
- </Set>
- </class>
- <class name="Role" table="T_role">
- <ID name="id">
- <generator class="native"/>
- </ID>
- <property name="name"/>
- </class>
- </hibernate-mapping></span>
The user class has a set set of role, and the set tag and the Many-to-many tag in the mapping file, so you can manipulate the role by using the user, but you cannot manipulate the data from role. Many-to-many relationship maintenance was used in the third table T_user_role. It holds the primary key for user and role.
Judging from the one-way, many-to-many association mappings above, I can check which roles a user belongs to, but I can't find out which user is in a role. So in order to solve this problem, we adopt bidirectional correlation mapping.
In fact, the two-way correlation mapping is to set a mapping relationship at the 2 end. That is, the set collection of the user is also added in role:
- <span style="font-size:18px" >/**
- * Class Category
- * @author Longxuan
- *
- */
- Public class Role {
- private int id;
- private String name;
- private set<user> users;
- //Omit get and set methods here
- }
- </span>
The role section in the mapping file also needs to be modified accordingly:
- <span style="font-size:18px"><class name="Role" table="T_role" >
- <ID name="id">
- <generator class="native"/>
- </ID>
- <property name="name"/>
- <set name="users" table="T_user_role">
- <key column="Roleid"></key>
- <many-to-many class="User" column="user_id"></many-to-many >
- </Set>
- </class></span>
It is important to note that the table and two column in the configuration file must be consistent. Otherwise it's going to go wrong. If the table name is different, then 2 intermediate tables will be generated, one with user maintenance and one with role. Because it becomes 2 many-to-many unidirectional association mappings. If the column names are inconsistent, they will be sent out. Or it becomes a 2-to-many one-way association map. At the same time, data redundancy is also occurring.
So the two-way correlation mapping, must ensure that the 2-terminal mapping relationship is set consistent. can be referred to as "bidirectional correlation mapping".
Eat a Mouthful of hibernate (vi)--many-to-many association mappings