• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bidirectional One-To-One using @PrimaryKeyJoinColumn

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[p-277 of EJB3 In Action Book] talks about only unidirectional one-to-one. How about bidirectional one-to-one using @PrimaryKeyJoinColumn? Do i have to specify
mapped-by attribute as i did it for bidirectional one-to-one using @JoinColumn.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any help on this ?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a bi-directional OneToOne, then you should always use mappedBy in the direction opposite to the foreign key constraint.
There are basically two pieces of information in the OneToOne, which foreign key field should be written to from the relationship, and whether the source or target should be inserted to the database first.

Using mappedBy, means don't write any foreign keys from the reference, and insert this object first.
Using a @PrimaryKeyJoinColumn means the object has foreign keys, but because it also maps them using basic Id attributes, don't write to any of the foreign keys from the reference, and insert the referenced object first.
Using a @JoinColumn means the object has a foreign key, write to it from the reference (unless it is insertable/updateable false), and insert the referenced object first.


http://en.wikibooks.org/wiki/Java_Persistence/OneToOne#Target_Foreign_Keys.2C_Primary_Key_Join_Columns.2C_Cascade_Primary_Keys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic