This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "about one to one" Watch "about one to one" New topic
Author

about one to one

David Wong
Greenhorn

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
 
Similar Threads
CMP maping - one to many unidirection in wl7.0
[Solved] [Problem] Referencing two objects with OneToOne relation
One to one mapping issue
mapping three tables
Composite foreign key mapping in Hibernate 3 using JPA