-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: One To One with shared primary key
PostPosted: Wed May 06, 2009 12:41 pm 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
I have 2 tables:

Person:
person_id: PK
attribute2:String


Address:
person_id:pFK
field1:String
....

I like 2 represent them in 2 entities (PersonEntity, AddressEnity) and there should be a one to one relation between them, stored in the PersonEnity (thats where the person_id is generated with a sequence generator).
The entites look like follows:

PersonEnity:
Code:
@Column(name = "person_id")
@Id
@SequenceGenerator(name = "personId", sequenceName = "seqPersonID", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "personId")
private Integer id


@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "person")
private AddressEntity address;



AddressEnity:
Code:

@Id
@GenericGenerator(name = "addressGenerator", strategy = "foreign", parameters = { @Parameter(name = "property", value = "person") })
@GeneratedValue(generator = "addressGenerator")
private Integer id;

@Id
@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private PersonEntity person;


As you see because of the GenericGenerator i needed to create a bidirectional one to one relation.

With this code I get following ERROR:

Unknown mappedBy in: ch.test.PersonEntity.address, referenced property unknown: ch.test.AddressEntity.person

How does I have to create this relation correctly?

The solution I found in the hibernat doc was: (see also http://docs.jboss.org/hibernate/stable/core/reference/en/html/assoc-bidirectional.html#assoc-bidirectional-121):
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</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>

But how can I do that with annotations?


Top
 Profile  
 
 Post subject: Re: One To One with shared primary key
PostPosted: Thu May 07, 2009 9:24 am 
Newbie

Joined: Tue Sep 09, 2008 11:35 am
Posts: 10
Ok, the problem was that I put the @ID annotation on the OneToOne relation. Removing this worked fine.

Code:
@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private PersonEntity person;


instead of:

Code:
@Id
@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private PersonEntity person;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.