-->
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: Lazy loading feature
PostPosted: Thu Jun 09, 2011 2:09 am 
Newbie

Joined: Thu Jun 09, 2011 1:59 am
Posts: 3
Hello.
I want to use Lazy loading, so I have next entitys:

Code:
class BigEntity {
  @ManyToOne(fetch = LAZY)
  @LazyToOne(NO_PROXY)
  private InnerEntity inner;
}


I want to get the next behavior:

Code:
BigEntity big = em.find(BigEntity.class, id);

hibernate: select * from BigEntity where id = ?
Code:
InnerEntity inner = big.getInner();

hibernate: select * from InnerEntity where id = ?

But I have next situation:
select * from InnerEntity where id = ? send to DB when I get some property of InnerEntity, but not when I call big.getInner();


Top
 Profile  
 
 Post subject: Re: Lazy loading feature
PostPosted: Thu Jun 09, 2011 3:39 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
So what is your problem. This is lazy loading... As soon as you want to get information about innerEntity it has to be fetched.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Lazy loading feature
PostPosted: Thu Jun 09, 2011 6:28 am 
Newbie

Joined: Thu Jun 09, 2011 1:59 am
Posts: 3
Thank you for replay/

I want hibernate to send select request, when I call
Code:
big.getInner()


but not when I call
Code:
big.getInner().getField()


Top
 Profile  
 
 Post subject: Re: Lazy loading feature
PostPosted: Thu Jun 09, 2011 10:39 pm 
Newbie

Joined: Wed Jun 01, 2011 1:16 am
Posts: 4
vrnroman wrote:
I want hibernate to send select request, when I call
Code:
big.getInner()



When you call
Code:
big.getInner()
you will get a proxy that is not initialized until you force it.

You could call
Code:
Hibernate.initialize(big.getInner())
to load whenever you want. Does that help?

http://docs.jboss.org/hibernate/core/3. ... ialization

What you're asking for specifically is called "no-proxy" fetching (see here: http://docs.jboss.org/hibernate/core/3. ... e-fetching). It requires bytecode implementation so you can say whether it is worth the trouble.


Top
 Profile  
 
 Post subject: Re: Lazy loading feature
PostPosted: Thu Jun 09, 2011 11:55 pm 
Newbie

Joined: Thu Jun 09, 2011 1:59 am
Posts: 3
Yes, you are right, initialing "by-hand" helps me:
Code:
Hibernate.initialize(big.getInner())


I think this solution "smells": there are too many places for this static method in project. But, as I see, no_proxy mechanism is deprecated. So I choose static method.

Thank you


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.