Configure the association between tables and tables in the. Hbm.xml mapping file in the Hibernate framework

Source: Internet
Author: User
Tags generator one table

The association relationship in 1.Hibernate can be divided into:

One-way relationship: Only one table points to another table.

Bidirectional relationship: Tables and tables can be pointed to each other.

2. Depending on the associated table, it can be divided into:

1 to 1 relations

1 to multiple relationships

Multi-Many-to-many relationships

For example: 1 customers can correspond to multiple orders, is 1 to many of the relationship.


(1) 1 pairs of multiple associations :

One -way Many-to-many 1: Using <many-to-one/> mapping, a foreign key column is added to the database.

Use the following code in a multiport. hbm.xml file:

<many-to-one name= "Multi-terminal entity class corresponding to property name" column= "foreign key column name" class= "Association class name (corresponding foreign key class name)"/>
one-way 1 to many: Using <many-to-one/> Mapping, foreign key columns are added in the database

Use the following code in the 1-port. hbm.xml File:

<set name= "1-Port corresponding property name" >
	<key column = "Foreign key column name"/>
	<one-to-many name= "Association class name"/>
</set>
bidirectional 1 to many: Use <set&gt at 1 end, use <many-to-one/&gt in multiport, and generally use this association in actual development


(2) 1 to 1 association : 4 Types (primary table: Table with foreign keys; foreign key table: Table with foreign key)

based on the foreign Key one-way association : a special one-way 1 relationship, multi-terminal is also 1

Use the following code in the corresponding entity class. hbm.xml file in the primary table:

<many-to-one name= "Property name" column= "foreign key column name" class= "Association table name" unique= "true" cascade= "all"/>

Unique= "true": 1 to 1, default to False for 1.

Cascade= "All": represents an operation on the associated object while the current entity object is being manipulated.

Bidirectional Association based on foreign key :

Foreign keys can be placed on either side, at the end of the foreign key to the use of <many-to-one/>, the property unique property value to see True, the external key corresponding to the other end of the set <one-to-one/> based on the primary key one-way association :

Define a primary key generation policy in the primary table corresponding to the entity class. Hbm.xml mapping File:

<!--foreign: represents generating primary key with associated entity-->
 <generator class= "foreign" >
            <param name= "Property" > Association property name </param> </generator> in current class
 

and set the <one-to-one/> association in the main table corresponding entity class. hbm.xml file:

<one-to-one name= "Property name" class= "Association class name (foreign key class name)" Constrained= "true"/>

Constrained= ' true ': The primary key of the datasheet that represents the current entity mapping is also used as a foreign key association to specify the data table for the entity class mapping.

Bidirectional Association based on primary key :

Both ends use <one-to-one/>, where the primary key generation policy for one end must be generated by the associated entity:

<generator class= "foreign" >
	<param name= "Property" > Associated attribute name in current class </param> </generator
>













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.