java Hibernate many-to-many mapping
Foreword:
1. One-way many-to-many
Examples of one-way many-to-many use people and positions For example, a person can have multiple positions, and a position can have multiple people. One-way many-to-many means that only one end can query and obtain the content of the other end. A many-to-many relationship will generate an association table before the object when generating the relationship model. The association table stores the primary keys of the two relationship tables. Their relationship is as follows:
Code part:
(1) Mapping and relationship class
Because it is a one-way relationship, it only needs to be maintained at one end, so we You need to add the
<span style="font-family:KaiTi_GB2312;font-size:18px;"><hibernate-mapping> <class name="com.bjpowernode.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"/> <many-to-many class="com.bjpowernode.hibernate.Role" column="role_id" /> </set> </class> </hibernate-mapping></span>
##Role.hbm.xml code is relatively simple and there is no need to add extra tags to maintain relationships:
<hibernate-mapping> <class name="com.bjpowernode.hibernate.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
<span style="font-family:KaiTi_GB2312;font-size:18px;">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 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; } }</span>
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; } }
## (2) Add and read data:
Add When data is generated, you need to first save the relationship to the database, then create the user Hash table, add the corresponding relationship to the hash table, and finally create the user and add the hash table to the user. What needs to be paid attention to in this part is the order of writing, otherwise there will be many null values, which is the same as the previous mapping.
public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Role r1 = new Role(); r1.setName("数据录入人员"); session.save(r1); Role r2 = new Role(); r2.setName("商务主管"); session.save(r2); Role r3 = new Role(); r3.setName("商务经理"); session.save(r3); Role r4 = new Role(); r4.setName("项目会计"); session.save(r4); User u1 = new User(); u1.setName("张三"); Set u1Roles = new HashSet(); u1Roles.add(r1); u1Roles.add(r2); u1.setRoles(u1Roles); session.save(u1); User u2 = new User(); u2.setName("李四"); Set u2Roles = new HashSet(); u2Roles.add(r1); u2Roles.add(r2); u2Roles.add(r3); u2.setRoles(u2Roles); session.save(u2); User u3 = new User(); u3.setName("王五"); Set u3Roles = new HashSet(); u3Roles.add(r3); u3Roles.add(r4); u3.setRoles(u3Roles); session.save(u3); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
When reading, because it is a one-way relationship, you only need to read the content of the other end through one, and read the role through user content. The code is as follows:
public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user = (User)session.load(User.class, 2); System.out.println(user.getName()); for (Iterator iter=user.getRoles().iterator(); iter.hasNext();) { Role role = (Role)iter.next(); System.out.println(role.getName()); } session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
2. Two-way many-to-many mapping
As introduced before, two-way many-to-many maintains the relationship at both ends at the same time, from either end The content can be loaded to the other end. Without further ado, let’s get straight to the code:
Because it is bidirectional, it is necessary to add bidirectional collection mapping at the same time. Add the
<hibernate-mapping> <class name="com.bjpowernode.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.bjpowernode.hibernate.User" column="user_id"/> </set> </class> </hibernate-mapping>
The User.hbm.xml code is as follows, which is the same as the one-way mapping code:
<hibernate-mapping>
<class name="com.bjpowernode.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.bjpowernode.hibernate.User" column="user_id"/>
</set>
</class>
</hibernate-mapping>
Role.java part, like the one-way user.java, you need to add a collection mapping set. The code is as follows:
import java.util.Set; 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; } }
User.hbm.xml The User.java code is the same as the code above, so I won’t put it all here.
Summary:
Through the introduction of several blogs, I believe everyone has understood one-way and multi-way. We only need to remember one-way and two-way. It is quite simple. of.
Thank you for reading, I hope it can help you, thank you for your support of this site!
For more java Hibernate many-to-many mapping details and example code related articles, please pay attention to the PHP Chinese website!