-->
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.  [ 3 posts ] 
Author Message
 Post subject: Bidirectional one-to-one relationship sql
PostPosted: Wed May 06, 2009 6:32 pm 
Newbie

Joined: Wed May 06, 2009 6:06 pm
Posts: 4
I have a bidirectional one-to-one relationship set up between two classes, A and B. A "owns" the relationship. It seems to me there is always one additional query than should be necessary to load these objects. After loading an object of type A and then calling A.getB() I get:

select
a0_.A_ID as A1_0_1_,
a0_.B_ID as B3_0_1_,
a0_.A_TEXT as A2_0_1_,
b1_.B_ID as B1_1_0_,
b1_.B_TEXT as B2_1_0_
from
A a0_
left outer join
B b1_
on a0_.B_ID=b1_.B_ID
where
a0_.A_ID=?

To me, since the relationship is bidirectional it would appear all necessary information is now present to fill in both A and B. However, this is followed by:

select
a0_.A_ID as A1_0_1_,
a0_.B_ID as B3_0_1_,
a0_.A_TEXT as A2_0_1_,
b1_.B_ID as B1_1_0_,
b1_.B_TEXT as B2_1_0_
from
A a0_
left outer join
B b1_
on a0_.B_ID=b1_.B_ID
where
a0_.B_ID=?

I thought there might be some relationship attribute that could change this behavior but so far after trying several I don't see it. Could anyone explain if there is a way to get this to run with a single query and if not why the second query is needed?

Thanks!


Top
 Profile  
 
 Post subject: Re: Bidirectional one-to-one relationship sql
PostPosted: Thu May 07, 2009 2:08 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
As relationship is by directional, one side of relationship should be
Quote:
inverse= true
or
Quote:
@MappedBy
.

If you post the code, that can give more understanding.


Top
 Profile  
 
 Post subject: Re: Bidirectional one-to-one relationship sql
PostPosted: Thu May 07, 2009 10:50 am 
Newbie

Joined: Wed May 06, 2009 6:06 pm
Posts: 4
I should add it is on a foreign key if that wasn't clear from the sql. I'm using annotations and here is where the relationship is defined. I did try it using mapping files and saw the same behavior.

Code:
@Entity
@Table(name = "A")
public class A
{
   @Id
   @GeneratedValue
   @Column(name = "A_ID")
   private Long id;
   
   @Column(name = "A_TEXT")
   private String text;
   
   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "B_ID")
   private B b;

        ...
}

@Entity
@Table(name = "B")
public class B
{
   @Id
   @GeneratedValue
   @Column(name = "B_ID")
   private Long id;
   
   @Column(name = "B_TEXT")
   private String text;
   
   @OneToOne(mappedBy = "b")
   private A a;

        ...

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.