Eat a Mouthful of hibernate (vi)--many-to-many association mappings

Source: Internet
Author: User

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:

  1. <span style="font-size:18px" >/**
  2. * Student Class
  3. * @author Longxuan
  4. *
  5. */
  6. Public class User {
  7. private int id;
  8. private String name;
  9. private set<role> roles;
  10. //Omit get and set methods here
  11. }
  12. /**
  13. * Class Category
  14. * @author Longxuan
  15. *
  16. */
  17. Public class Role {
  18. private int id;
  19. private String name;
  20. //Omit get and set methods here
  21. }
  22. </span>


Map file:

  1. <span style="font-size:18px"><? XML version="1.0"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Com.bjpowernode.hibernate">
  6. <class name="User" table="T_user">
  7. <ID name="id">
  8. <generator class="native"/>
  9. </ID>
  10. <property name="name"/>
  11. <set name= "roles" table="T_user_role">
  12. <key column="user_id"></key>
  13. <many-to-many class="Role" column="Roleid"></many-to-many>
  14. </Set>
  15. </class>
  16. <class name="Role" table="T_role">
  17. <ID name="id">
  18. <generator class="native"/>
  19. </ID>
  20. <property name="name"/>
  21. </class>
  22. </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:

  1. <span style="font-size:18px" >/**
  2. * Class Category
  3. * @author Longxuan
  4. *
  5. */
  6. Public class Role {
  7. private int id;
  8. private String name;
  9. private set<user> users;
  10. //Omit get and set methods here
  11. }
  12. </span>


The role section in the mapping file also needs to be modified accordingly:

  1. <span style="font-size:18px"><class name="Role" table="T_role" >
  2. <ID name="id">
  3. <generator class="native"/>
  4. </ID>
  5. <property name="name"/>
  6. <set name="users" table="T_user_role">
  7. <key column="Roleid"></key>
  8. <many-to-many class="User" column="user_id"></many-to-many >
  9. </Set>
  10. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.