-->
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.  [ 5 posts ] 
Author Message
 Post subject: Entity is loaded twice in bidirection OneToOne mapping
PostPosted: Mon May 12, 2008 7:06 pm 
Newbie

Joined: Mon May 12, 2008 6:55 pm
Posts: 3
Hi all,

I am using hibernate with JPA and have come across something that looks a bit strange to me. I have two entities that have a OneToOne bi directional mapping between them. When I do a query on the owning entity, it gets loaded twice.

Short version of my entities are;

Code:
@Entity
public class One {

   @Id
   @GeneratedValue
   private long id;
   
   @OneToOne
   private Two two;

   private int counting;
}

@Entity
public class Two {

   @Id
   @GeneratedValue
   private long id;
   
   @OneToOne(mappedBy="two")
   private One one;
}

When I perform the query
Code:
select o from One as o  left join fetch o.two where o.counting > 50


hibernate executes the following sql
Code:
Hibernate: select one0_.id as id0_0_, two1_.id as id1_1_, one0_.counting as counting0_0_, one0_.two_id as two3_0_0_ from One one0_ left outer join Two two1_ on one0_.two_id=two1_.id where one0_.counting>50


and then the following for every entity returned in the first query.
Code:
Hibernate: select one0_.id as id0_1_, one0_.counting as counting0_1_, one0_.two_id as two3_0_1_, two1_.id as id1_0_ from One one0_ left outer join Two two1_ on one0_.two_id=two1_.id where one0_.two_id=?


What I don't understand is why that is the case, as the One entity has already been loaded by the first query and Two.one is mearly just pointing back at it.

Am I missing something?

Thanks
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 5:13 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
read this http://www.hibernate.org/162.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 6:03 am 
Newbie

Joined: Mon May 12, 2008 6:55 pm
Posts: 3
I'm not sure how that applies to be honest.

My associations are not lazy, and also they are not one-to-many or many-to-one, they are one-to-one.

The point I'm making is that the Entity one has already been loaded for the assoication as it was loaded in the first query. It is using mappedBy so they must be the same entity. So why is it performing a second query to the database when it already has the entity for that assoication?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 6:05 am 
Newbie

Joined: Mon May 12, 2008 6:55 pm
Posts: 3
..


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 4:42 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
in hibernate documentation - one-to-one : lazy (optional - defaults to proxy) - so your association appears to be lazy and in article i had posted is explained why two selects are performed


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