Hibernate ing (association)

Source: Internet
Author: User

1. Unidirectional N-1 Association

1. One-way N-1 Association, for example, multiple people correspond to the same address, you only need to find the corresponding address entity from the human object, do not care about all the households of a specific address.

2. To enable two persistence classes to support this association ing, the program should add a correlated entity that references 1 end at one end of N. As follows:

Public class Person {... private Address address Address ;.....}

3. At the same time in the configuration of hbm files are divided into a non-connection table and a connection table (General N-1 do not need to connect to the table), 1 end in the configuration of hbm files to maintain normal configuration. Person. hbm. xml is as follows:














 

Ii. Unidirectional 1-1 Association

1. One-way 1-1 and one-way N-1 persistence class is no difference, as shown above.

2. The ing configuration of unidirectional 1-1 and unidirectional N-1 is also very similar The unique = true attribute is added to the element to indicate that one end of N is unique.












Address





 

3. Unidirectional 1-N Association

1. The persistence of one-way 1-N Association has changed. The set attribute must be used in the persistence class, because the end of N will be expressed as a set.

Private Set

Addresses = new HashSet (); // use person. getAddresses (). add (address) when assigning values );

 

2. When ing set attributes from 1 to n, you must add Child meta maps to foreign key columns.












 

4. Association of unidirectional N-N

1. One-way N-N Association and 1-n association of the persistence class is exactly the same, add a set attribute at one end of the control relationship

2. The N-N association must use a connector table, and the N-N Association is very similar to the 1-N association with a connector table, as long as you remove Unique = true in the element.

Bidirectional 1-N (bidirectional N-1) Association

1. two-way 1-N Association and two-way N-1 Association are the same two cases, both ends need to add access to the association attribute, N end add reference to the association object attributes, add the set attribute at one end of 1.

Public class Person {... private Set

Addresses = new HashSet ();.....}

 

Public class Address {... private Person person Person ;.....}

2. Bidirectional 1-N Association for non-connection tables


.....................






 


................


 

3. Bidirectional 1-N association with a connection table


....................






 


.........................







 

6. Bidirectional N-N Association

1. Bidirectional N-N Association requires both ends to use the set property, both ends add access to the set property.

2. Two-way N-N association can only be used to establish the relationship between entities.


....................






 


....................







 

VII. Bidirectional 1-1 Association

1. For bidirectional 1-1 join, you only need to add the attribute that references the correlated object in both persistence classes.

2. Bidirectional 1-1 association based on foreign keys


....................


.....................

2. Bidirectional 1-1 association based on primary keys


....................






Person


....................


 

3. Bidirectional 1-1 association with a connection table


.....................






 


............................







 

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.