Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
17Activity
0 of .
Results for:
No results containing your search query
P. 1
Hibernate Association Mappings

Hibernate Association Mappings

Ratings: (0)|Views: 1,478|Likes:
Published by Naveen Kumar

More info:

Published by: Naveen Kumar on Mar 06, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

01/25/2013

pdf

text

original

 
Hibernate Association MappingsIntroduction
Association mappings are the often most difficult thing to get right. In this section we'll go through thecanonical cases one by one, starting with unidirectional mappings, and then considering the bidirectional cases.We'll use Person and Address in all the examples.We'll classify associations by whether or not they map to an intervening join table, and by multiplicity. Nullable foreign keys are not considered good practice in traditional data modelling, so all our examples use notnull foreign keys. This is not a requirement of Hibernate, and the mappings will all work if you drop thenullability constraints.
Unidirectional associationsMany- to-One:
A
unidirectional many-to-one association
is the most common kind of unidirectional association.<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:
A
unidirectional one-to-one association on a foreign key
is almost identical. The only difference is the columnunique 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
unidirectional one-to-one association on a primary key
usually uses a special id generator. (Notice that we'vereversed the direction of the association in this example.)<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:
A
unidirectional one-to-many association on a foreign key
is a very unusual case, and is not reallyrecommended.<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 ) 
 
We think it's better to use a join table for this kind of association.
Unidirectional associations with join tablesOne-to-Many:
A
unidirectional one-to-many association on a join table
is much preferred. Notice that by specifyingunique="true", we have changed 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>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 )
Many- to-One:
A
unidirectional many-to-one association on a join table
is quite common when the association is optional.<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>

Activity (17)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
goutham28 liked this
vntkhatri liked this
Alok Sharma liked this
ilangovan_v liked this
gopalmyneni liked this
santoshmca165 liked this
spraveen23 liked this
madhuneelam5558 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->