JPA or hibernate annotations – one to one mapping using shared primary key

The example given in hibernate documention for a shared primary key doesn’t work while inserting records. The other end of the one to one relationship gets inserted with a primary key of zero (if the id is primitive) or the insertion will totally fail if the id is reference type. Use this solution for one to one shared primary key:

Note that for this approach to work you’ll have to use a bidirectional relationship. (No harm in doing so, just involves a little extra coding). On both ends (lets call them A and B), you’ll have the @OneToOne. On one end “B” we will have a mappedBy attribute mentioning the name of the property on the other end “A” where the mapping is mentioned (similar to inverse=”true”). The same “B” will also have a generator on ‘id’ that will use the ‘id’ on property pointing to end “A”. One end “A” you can use a sequence to generate the ‘id’ and the same value will be inserted as the ‘id’ for B also (hence the name ‘shared key’). Here is the code:

public class MemberComment

@SequenceGenerator(name=”MEMBERCOMMENTID_SEQ”, sequenceName=”MEMBERCOMMENTID_SEQ”,allocationSize=1)
long memberCommentId;

@PrimaryKeyJoinColumn //Hibernate will know that it will have to look for a record with the same primaryKey in the other end while retrieving by looking at this annotation
private MemberCommentCode memberCommentCode;



public class MemberCommentCode

@GeneratedValue( generator = “customGen” )
@GenericGenerator( name = “customGen”, strategy = “foreign”, parameters = @Parameter( name = “property”, value = “memberComment” ) ) //For using the id from memberComment property- this property needs to be set before saving this record.
long memberCommentCodeId;

@OneToOne(mappedBy=”memberCommentCode”)//tells hibernate to ignore saving this record while saving the ‘MemberCommentCode’ record, thus preventing double insertions – one while inserting MemberComment another while inserting MemberCommentCode
@PrimaryKeyJoinColumn//tells hibernate to use primaryKeys for joining during retrieval
private MemberComment memberComment;




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: