Hibernate object Relationship Implementation (c) Many-to-many implementations

Source: Internet
Author: User

One-way n-n: (Catogory-item) A category that corresponds to multiple entries, one entry for multiple categories

A. Take the reference to a collection with entries in the category class as an example:

B. Representation in the database: Create a new table with the foreign key associated with the primary key of the category and the entry to make the new table's federated primary key.

C. Mapping configuration: Configure a Foreign key mapping relationship in the category table

D Test:

Save

A four INSERT statement will be executed to insert the kind and entry and then execute four INSERT statements to insert the association relationship into the Category_item table;

Get:

Gets a collection of category association objects that are lazy loaded;

When getting the item collection data, an inner join is used, that is, the relationship table and the Entry table are connected,

Two. For bidirectional many-to-many, add on a one-way basis

A. The item class in the table adds a collection reference for the category:

C. Entry mapping Table add mapping Relationship:

Special Note : bidirectional Many-to-many, one side <set> elements need to set inverse=true, only a party to maintain the association, or a primary key conflict (because there will be duplicate insert record),

The above configuration has not been added, need to add on ;

Hibernate object Relationship Implementation (c) Many-to-many implementations

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.