Hibernate Item Association

Source: Internet
Author: User
Tags generator

Multi-pair (many to one)
One-way many-to-one associations are the most common one-way relationships.

<class name= "Person" >
<id name= "id" column= "personId" >
<generator class= "native"/>
</id>
<many-to-one name= "Address"
Column= "Addressid"
Not-null= "true"/>
</class>

<class name= "Address" >
<id name= "id" column= "addressid" >
<generator class= "native"/>
</id>
</class>
CREATE TABLE person (personId bigint not null primary key, ADDRESSID
bigint NOT NULL)
CREATE TABLE Address (Addressid bigint not null primary key)

One-to-one (one to one)
The one-way and one-way associations based on the Foreign Key association are almost the same as the unidirectional many-to-one association. The only difference is the single
The foreign key field in a one-to-one association has a uniqueness constraint .

<class name= "Person" >
<id name= "id" column= "personId" >
<generator class= "native"/>
</id>
<many-to-one name= "Address"
Column= "Addressid"
Unique= "true"
Not-null= "true"/>
</class>

<class name= "Address" >
<id name= "id" column= "addressid" >
<generator class= "native"/>
</id>
</class>
CREATE TABLE person (personId bigint not null primary key, ADDRESSID
bigint NOT null unique)
CREATE TABLE Address (Addressid bigint not null primary key)

A one-way association based on a primary key association typically uses a specific ID generator. (Please note that in this case
We swap the direction of the association in the child. )

<class name= "Person" >
<id name= "id" column= "personId" >
<generator class= "native"/>
</id>
</class>

<class name= "Address" >
<id name= "id" column= "personId" >
<generator class= "foreign" >
<param name= "Property" >person</param>
</generator>
</id>
<one-to-one name= "Person" constrained= "true"/>
</class>
CREATE TABLE person (personId bigint not null primary key)
CREATE TABLE Address (personId bigint not null primary key)

One-to-many (one to many)
A one-to-many association based on a foreign key association is a rare case and is not recommended.

<class name= "Person" >
<id name= "id" column= "personId" >
<generator class= "native"/>
</id>
<set name= "Addresses" >
<key column= "PersonId"
Not-null= "true"/>
<one-to-many class= "Address"/>
</set>
</class>

<class name= "Address",
<id name= "id" column= "addressid";
<generator class= "native"/>
</id>
</class>
CREATE table person (personId bigint not null primary key)
CREATE TABLE Address (Addressid bigint NOT null primary key, PERSONID
bigint not NULL)

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.