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)