Hibernate affinity Configuration (one-to-many, one-to-one, and many-to-many)

Source: Internet
Author: User

First Association relationship: one-to-many (many-to-two)

"One-to-many" is the most common mapping relationship, simply like the relationship between consumers and orders.

One -to-many : from the consumer corner of the degree of a consumer can have multiple orders, that is, a pair of more.

Many-to-one : From the point of order, multiple orders can correspond to a consumer, that is, many to one.

Configuration information for a one-to-many relationship in HBM files:

Consumer (Party):

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/  Hibernate-mapping-3.0.dtd "> 

Order (multiple):

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "> 

  

A one-to-many relationship, the customer side corresponds to multiple order parties, so the customer contains a collection for storing multiple order,order that contain a customer that stores the associated person.

A one-to-many association relationship has a special case: itself a-to-many association. For example:

The hbm file setting for itself one-to-many correlation itself:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "> 

  

The foreign key holds the father's primary key.

The second relationship: many-to-many

Many-to-many relationships are also common, such as the relationship between students and electives, where a student can choose multiple elective courses, and each elective can be selected by more than one student. Many-to-many association relationships in a database are generally handled in the form of intermediate tables, converting many-to-many to two-to-many.

Many-to-many relationships between data tables such as:

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

Students:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Course:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

  

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 course table, it is associated with the student table using an intermediate table Student_course. If you want to get the student record for the course record, you first need to use the foreign key "course_id" to get the corresponding data from the Student_course table, and then use the value of the "student_id" column in the obtained data. Retrieves the relevant student data in the student table. In fact, for ease of understanding, you can use the course table as a student table and vice versa. This allows you to use a one-to-many mindset to understand that a multi-party relationship requires a foreign key so in this case you need "course_id" to close.


Third relationship: one to one
One to one relationship is the relationship between the team and the location of the team, a team only has an address, and a region has only one team (seemingly a little reluctant to do it). There are two kinds of data table-to-one relationships: a foreign key association and a primary key association. This is illustrated below:

One-to-one foreign key association:

Two-to-one primary key association: requires that the primary key of both tables be exactly the same, and that the correlation is established by the primary key of the two tables:

One-to-one foreign key association configuration information in HBM files:

Address:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Team:

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

  

A one-to-one foreign key association, in fact, can be seen as a pair of a special form, the multi-party degenerate into a. The multi-party degenerate into one needs to set "unique" = "true" in the <many-to-one> tag.
A pair of primary keys is associated with the configuration information in the HBM file:

Address:

Team:

  

A pair of primary key associations, which is the same as the primary key value for the two sheets. To make the primary key of the two tables the same, only one table generates the primary key, and the other table references the primary key.

<generator class= "foreign" >

<param name= "Property" >adress</param>

</generator>

"Class" = "foreign" is the primary key value that sets the primary key reference adress property of the team table.

Hibernate affinity Configuration (one-to-many, one-to-one, and 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.