-->
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: @OneToOne (fetch = FetchType.LAZY) not lazy load
PostPosted: Tue Mar 02, 2010 8:47 am 
Newbie

Joined: Fri Jun 08, 2007 8:07 am
Posts: 4
Hello.
I'm using jpa + hibernate (3.3.2.GA).
I mapped relations as follows:
-------------------------------
Code:
public class A {
...
@OneToOne(
          optional = true,
          cascade = {CascadeType.PERSIST, CascadeType.REFRESH },
          fetch = FetchType.LAZY,
          mappedBy = "a" )
private B b = null;
...
}

public class B {

...

@ManyToOne(
          optional = false,
          cascade = {CascadeType.PERSIST, CascadeType.REFRESH },
          fetch = FetchType.LAZY
          )
  @JoinColumn (name = "ID_A", unique = true)
  private A a= null;
...
}



-------------------------------
When I consult the entity "a", made just after the query corresponding to "b".
select ... from A ...
select ... from B ...

Why does not lazy load?

I tried also with the labels:
----------------------
Code:
@ LazyToOne (LazyToOneOption.PROXY)
@ org.hibernate.annotations.Fetch (FetchMode.SELECT)

----------------------

and I do not work. ¿Why?


Thank you!


Top
 Profile  
 
 Post subject: Re: @OneToOne (fetch = FetchType.LAZY) not lazy load
PostPosted: Tue Mar 02, 2010 10:12 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Me itself stumbled over this problem some time ago

Fact is that @OneToOne in combination with the mappedBy (=inverse) attribute don't works lazy.
I explain you why.

First you have to understand that a hibernate proxy needs at least the primary key information about the target entity.
Now in case of @OneToOne with mappedBy attribute in your source entity record there's no information about the primary key of the associated entity, so hibernate needs to perform a query first in order to can create the proxy.

Solutions:
1. declare the other side of the association as inverse-owner (mappedBy)

You may also try the proxy-with-interception approach.
I know that it exists, but I have no experience with it.


Top
 Profile  
 
 Post subject: Re: @OneToOne (fetch = FetchType.LAZY) not lazy load
PostPosted: Wed Mar 03, 2010 5:06 am 
Newbie

Joined: Fri Jun 08, 2007 8:07 am
Posts: 4
thanks!


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.