-->
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: Lazy loading & 2nd level cache
PostPosted: Fri Sep 12, 2008 7:53 am 
Newbie

Joined: Thu Mar 13, 2008 5:59 am
Posts: 3
Hi,

I have an entity Country, which is used in many places in my application (Person, Address, ...). As I know there is only a small number of countries (less than 250), I want to have all Country instances in the second level cache.

Now, if I query a Person instance, I want Hibernate to not query the database for the country property, but take it from the cache.

I can achieve this by defining the country property as lazily loaded, then access the property, and it will be read from the cache.

But I want to eagerly load the country property. This always creates SQL containing a "left outer join". Is there a way to instruct Hibernate to use the 2nd level cache in this case? Something like: "For this property, I know that the instance should be in the 2nd level cache, so do not read it in the SQL query, only read the primary key and fetch the instance from the cache. Do a second select in case the cache is not yet fully populated".

Hibernate version: 3.3.0SP1

Thanks,

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 8:04 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
As far as I remember, you should mark the Country class as "lazy" in the class mapping. Something like:

Code:
class name="..." table="..." lazy="true"


This will tell Hibernate to create a proxy for it when referenced from another entity (though a many-to-one relationship). Note: "true" might already by the default, but certainly don't set it to false...

You can (or should - don't remember), also mark the many-to-one relationship as lazy as well (it might be false per default) - provided it is not nullable (see Hibernate doc on the subject).


With all this set, loading an Employee won't hit the Country table. Instead, the firstime the getCountry() method is invoked, Hibernate will attempt to load the Country by ID - which will be retrieved from the second level cache (provided you have a cache it).

Hope it helps..

_________________
If you found my post helpful, please rate it! Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 4:30 am 
Newbie

Joined: Tue Nov 18, 2008 4:12 am
Posts: 8
Location: Singapore
Hi,

Just wondering...wouldn't it be enough to simply enable second-level caching for the Country entity (ie add <cache usage="read-write" /> to the hibernate mapping for Country)?

You did mention that a SELECT query is allowed if the cache is not fully populated (ie if the country record is not found in the cache). Doing the above will ensure that the Country entity will remain in and always be loaded from the 2nd-level cache after the first SELECT for each record.

_________________
Life at i-flex turns me on!


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.