-->
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.  [ 6 posts ] 
Author Message
 Post subject: Lazy initialization and Proxying
PostPosted: Sun Nov 09, 2003 1:24 am 
Newbie

Joined: Mon Oct 13, 2003 8:59 am
Posts: 3
Location: Hyderabad
Can anybody tell how can I get advantage of lazy initialization?
How the Proxy can be used to laziely intialize?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 10:58 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Suppose you have an Employee class and every employee has a set of Skills associated with him. This set may be large enough (say every employee has about 50 skills). In the Employee class mapping you write:

Code:
        <set name="skills"
             table="XXX"
             inverse="true">

            <key column="EMPLOYEE_ID"/>
            <one-to-many class="Skill"/>
        </set>


This works but has one small problem - when you load Employee, Hibernate makes extra effort to load these 50 skills as well to make skills set imeediately available. Not a big problem of course, but probably in the most cases when you are loading Employee, you do not need these skills at all. Skills are required only for very rarely used operations. Why to load them every time? Lazy collection loading helps there. Just by adding lazy="true" attribute you tell Hibernale to postpone loadin of the skill set until it required.

In reverse direction every Skill has association to its employee (it has "employee" property of Employee class. Since Hibernate always tries to load averything associated with requested object, when you try to load one of Skills, Hibernate will load its Employee too. Moreover, when you load large list of Skills matching some criteria (skill name for instance), Hibernate will load Employee for each skill. This is usually a big overkill. You can avoid this by specifying proxy for Employee.

For me personally, it is good to specify lazy="true" for every <set> and to specify proxy for every class (which is not a set element)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 12:58 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
dimas wrote:
For me personally, it is good to specify lazy="true" for every <set> and to specify proxy for every class (which is not a set element)


Say class A has a collection of class B. The B set is lazy in A.

Will it work if I also specify a proxy for classes which are set elements (lazy) in other classes?

Can I declare a proxy for B?
Because I may have a class C referring many-to-one to class B

thx,
steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 1:37 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Yes, you can specify a proxy for class B.


Top
 Profile  
 
 Post subject: What about queries?
PostPosted: Fri Nov 14, 2003 2:09 pm 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Does the proxies works if we use a query instead of a direct load? Does queries respect the lazy initializations? And does criteria queries respect those initializations too?

Joao Rangel


Top
 Profile  
 
 Post subject: Re: What about queries?
PostPosted: Fri Nov 14, 2003 2:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jpgr wrote:
Does the proxies works if we use a query instead of a direct load? Does queries respect the lazy initializations?

Yes, yes and left join fetch can bypass it (see the doc)

jpgr wrote:
And does criteria queries respect those initializations too?

I would be surprised if it does not.

_________________
Emmanuel


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