-->
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.  [ 1 post ] 
Author Message
 Post subject: why @OneToOne( mappedBy ) not using mappedBy's @JoinColumn ?
PostPosted: Sat Dec 11, 2010 8:19 am 
Newbie

Joined: Sat Dec 11, 2010 7:20 am
Posts: 1
Hi,

Using Hibernate 3.5.6, with the following:

Code:
class A
{
   @Id
   private long id;

   private long aUniqueId;

   @OneToOne( mappedBy="a")
   private B b;
}

class B
{
   @Id
   private long id;

   private long bUniqueId;

   @OneToOne
   @JoinColumn( name="aId", referencedColumnName="aUniqueId" )
   private A a;  // column (aId) is a foreign key to A.aUniqueId
}

So to join the two tables, the join should be like:
Code:
from A a0 left outer join B b1 on a0.aUniqueId=b1.aId

However, I noticed in that the org.hibernate.type.OneToOne class for A will not actually use B's @JoinColumn but will always use A's primary key so the query incorrectly becomes:
Code:
from A a0 left outer join B b1 on a0.id=b1.aId

I got around this issue by doing the following change for A.b's annotation:

Code:
   @OneToOne
   @JoinColumn( name="aUniqueId", referencedColumnName="aId", insertable=false, updateable=false )
   private B b;

Hibernate is converting the @OneToOne relationship into a @ManyToOne when the @JoinColumn is present. I don't mind that on B, but I do mind it on A because it does introduce an extra table join in the query:
Code:
from A a0 left outer join B b1 on a0.aUniqueId=b1.aId left outer join A a2 on b1.aId=a2.aUniqueId

I would like to use @OneToOne( mappedBy="a" ) for A. Any suggestions? Is this a bug/issue in Hibernate because the OneToOneType doesn't use the mappedBy's @JoinColumn?

thanks,
Trev


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

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.