Hibernate mapping Relationship-many-to-many

Source: Internet
Author: User

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 Use table to implement ,

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:

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 ">        
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 ">        

Then the resulting database table


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 one-to-many. 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 withUserTable, it is associated withroleThe table uses an intermediate table User_role Association. If you want to getUserRecord the corresponding roleof theRecord, you first need to use the foreign key "UserID" fromUser_roleData in the table, and then use therOleid Column to retrieve the associated RO in the role table.leData. In fact, for the sake of understanding, you can use the user table in the use of the intermediate table as aroleTable, and vice versa. So you can use a couple of ideas to understand.


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


Hibernate mapping Relationship-many-to-many

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.