Hibernate mapping Relationship-many-to-many

Source: Internet
Author: User
Tags generator

Then on a few blogs, hibernate mapping relationship, today we talk about the last of the basic relationship: many-to-many.


one, many-to-many relationships

phenomenon: Many-to-many relationship, in life is very common in the relationship between the students and elective courses, a student can choose more than one elective course, and each elective course can be more than a choice of students. There is a lot of examples where a user can have multiple roles, a character can have more than one person, and so on.

Hibernate Implementation : Many-to-many relationships are implemented using the third table, converting many-to-many to two-to-many.

Of course, many-to-many also have a direction. Take the user-character to draw the class diagram.

1. One-way Association--User Association role

Configuration information for many-to-many relationships in HBM files:

Character 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 ">
        
User.hbm.xml Information:

          <?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 database table is shown in figure:


2. User-Role Bidirectional association:

The class diagram is as follows;

The configuration information is as follows

The Role.hbm.xml configuration is as follows:

        <?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 User.hbm.xml is configured as follows;
        <?xml version= "1.0"?>
        <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
	   "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
        


3. Unidirectional and bidirectional

In fact, it is not difficult to find that when the relationship from one-way to two-way, the user's configuration file has not changed, role of the configuration file more <set> tags. The resulting database is the same, except that there is a difference between accessing the database through the entity, and only one party that maintains the relationship can access the other.


Second, compare a pair of more


In fact, many is more than two one-to-many, its configuration is nothing novel relative to a pair of more. In a many-to-many relationship design, it is common to use an intermediate table to split them into two-to-many. The "table" attribute in the <set> tag is used to specify the intermediate table. The intermediate table typically contains a primary key value for two tables, which is used to store the relationship between the two tables. Since it was split into two one-to-many, the intermediate table is multiparty, which is used to specify the foreign key using the foreign key associated with the,<key>, which is used to fetch the corresponding data from the intermediate table. Each row of data in the intermediate table contains only the primary key of two relational tables, and to get the collection of objects associated with it, you also need to take out another primary key value from the record obtained by the foreign key, and fetch the data from the corresponding table and populate it into the collection. The "column" attribute in <many-to-many> is used to specify the corresponding data to be obtained by the value of that column.


For example, with the user table, it is associated with a role table using an intermediate table User_role. If you want to get a record of the role of the user record, you first need to use the foreign key "UserID" to obtain the corresponding data from the User_role table, and then use the value of the "Roleid" column in the obtained data to retrieve the relevant role data in the role table. In fact, for the sake of understanding, you can use the user table to think of the intermediate table as a role table, and vice versa. So you can use a one-to-many thinking to understand.


A simple mapping between objects here, in the next lesson, we say inheritance mapping.


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.