Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
54
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

Unidirectional associations with join tables

<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 )

You should instead use a join table for this kind of association.

8.3. Unidirectional associations with join tables

8.3.1. One-to-many

A unidirectional one-to-many association on a join table is the preferred option. Specifying unique="true", changes the multiplicity from many-to-many to one-to-many.

<class name="Person">

<id name="id" column="personId">

<generator class="native"/> </id>

<set name="addresses" table="PersonAddress"> <key column="personId"/>

<many-to-many column="addressId" unique="true" class="Address"/>

</set> </class>

<class name="Address">

<id name="id" column="addressId"> <generator class="native"/>

</id> </class>

187

Chapter 8. Association Mappings

create table

Person ( personId bigint

not

null primary key )

 

create

table

PersonAddress ( personId

not

null, addressId bigint not null primary key )

create

table

Address ( addressId bigint not null primary key

)

 

 

 

 

 

 

8.3.2. Many-to-one

A unidirectional many-to-one association on a join table is common when the association is

optional. For example:

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

<join table="PersonAddress"

optional="true">

<key column="personId" unique="true"/> <many-to-one name="address"

column="addressId"

not-null="true"/>

</join> </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

PersonAddress ( personId

bigint not

null primary key, addressId bigint not null )

create

table

Address ( addressId bigint not null

primary key

)

 

 

 

 

 

 

8.3.3. One-to-one

A unidirectional one-to-one association on a join table is possible, but extremely unusual.

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

<join table="PersonAddress"

optional="true">

<key column="personId"

unique="true"/>

<many-to-one name="address"

188

Many-to-many

column="addressId" not-null="true" unique="true"/>

</join> </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 PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )

8.3.4. Many-to-many

Finally, here is an example of a unidirectional many-to-many association.

<class name="Person">

<id name="id" column="personId">

<generator class="native"/> </id>

<set name="addresses" table="PersonAddress"> <key column="personId"/>

<many-to-many column="addressId" 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 PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )

create table Address ( addressId bigint not null primary key )

189

Соседние файлы в папке more