"Java EE Learning Day 46th" "Hibernate learning the third day" "Many-to-many relationship mapping"

Source: Internet
Author: User

I. Overview of many-to-many relationships

Take the relationship between the student and the curriculum as an example.

1. There are three tables involved in a many-to-many relationship, two entity tables, and a table dedicated to maintaining relationships.

2. The two entity classes in a many-to-many relationship should add the properties of each other's set collection separately and provide the set and get methods.

3. It is more complex to configure a mapping file than a one-to-many relationship.

Second, the mapping file

<Setname= "Students"Table= "Course_stu"Cascade= "Save-update"Inverse= "true">        <!--now the inverse property is set to true so that the relationship between course and Stu cannot be maintained through the course configuration file. -            <Key>                <columnname= "CID"></column>            </Key>            <Many-to-manyclass= "Com.kdyzm.moretomore.domain.Stu">                <columnname= "Sid"></column>            </Many-to-many>        </Set>

Many-to-many relational mapping files are written to explain:

Set Label:

Name property: property names for set collections in persisted classes

Table property: Tables Name of the intermediate table

Cascade Property: An important property that transforms the state of a cascading object from other states to a persisted state without throwing an exception.

Inverse property: Configures whether the relationship is maintained, if true, the relationship is not maintained, and if it is false, the relationship is maintained.

Key tag: Sets the foreign key, which is the foreign key in the intermediate table Course_stu table, indicating that the current table can be found through this field through the Course_stu table.

Many-to-many Tags: Setting association tables

Class Property: Sets the persisted class corresponding to the associated table

Column property: Sets the foreign key for the associated table that corresponds to the intermediate table.

"Java EE Learning Day 46th" "Hibernate learning the third day" "Many-to-many relationship 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.