File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes about one to one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "about one to one" Watch "about one to one" New topic

about one to one

David Wong

Joined: Dec 31, 2007
Posts: 15
are there two strategies of mapping for <one-to-one/> ?one is primary key mapping strategy other is foreign key mapping strategy.what's the advantage in each of them?

if i have a construction of db below:

table a
id int primary key
name varchar

table b

id int primary key
a_id int foreign key(a_id) references a(id)

how could we use the two strateies to get one a to one b.what are the differences?

thanks very much!!

Bitter Java For Better Life.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

I'm not sure how you're using the term one-to-one here?

To map one table to one class, is fairly straight forward. You can just use the @Entity annotation:

Mapping One Class to One Table

But you mentioned using a shared primary key? So this would be two classes to one table? You can use the @Embedded and @Embeddable annotations to accomplish this:

Mapping Two Classes to One Table Using Hibernate and JPA Annotations

But then you mention foreign keys? So you may have two tables related by a foreign key mapping to one class? In this case you're looking at using the @SecondaryTable annotation

Mapping One Class to Two Database Tables using Hibernate and JPA

Is that what you were getting at?

-Cameron McKenzie
subject: about one to one
It's not a secret anymore!