-->
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: Understanding 2nd level cache
PostPosted: Mon Mar 30, 2009 3:00 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
If I use session.getObject (MyClass.class, myid), then hibernate will use the 2nd level cache with no hit to the database.

However, if I use a HQL query "from MyClass where id in (:idList)" then it will always hit the database.

Is there a way with an idList to have the ability for hibernate to use the 2nd level cache instead of hitting the database all the time?

It also raises another question: why isn't there a method called

getObjects (Class clazzz, List<Serializable> idList) in the Session interface?

Dino


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 12:28 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Do you use the query cache?

In hibernate.cfg.xml:
<property name="cache.use_query_cache">true</property>

And then for the query you wish to cache set it as cacheable:
session.createQuery("from MyClass where id in (:idList)").setCacheable(true)

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 10:42 am 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
I don't want to cache the actual query since the idList may vary. This makes caching the query impractical since the combination of the idList may be huge.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 2:01 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
HQL queries are not really "understood" by Hibernate at that level. The only thing that Hibernate can do is to convert the HQL to SQL and then let the database return the result. What you can do to improve performance a bit to use Query.iterate(). This will make Hibernate to issue a query that only selects the primary key in the main query. Then, if the entity is found in the second-level cache it is fully loaded from there. This strategy is fine as long as most of the entities can be found in the second-level cache. If not, Hibernate will issue additional select:s against the database to load the missing entities.

By the way... if you are sure that most entities are already in the second-level cache it may be even faster to simply iterate over the list and use Session.get() or Session.load() for each id. Session.load() is probably very fast if you have enabled proxies for MyClass since it will simply create a new proxy for it (never hitting the database). Which method that works out the best probably also depends on what you are going to do with the objects later on. For example, if you expect the proxies to become initialized or not.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 4:05 pm 
Beginner
Beginner

Joined: Sun Nov 07, 2004 4:19 pm
Posts: 38
[quote="nordborg"]HQL queries are not really "understood" by Hibernate at that level. The only thing that Hibernate can do is to convert the HQL to SQL and then let the database return the result. What you can do to improve performance a bit to use Query.iterate(). This will make Hibernate to issue a query that only selects the primary key in the main query. Then, if the entity is found in the second-level cache it is fully loaded from there. This strategy is fine as long as most of the entities can be found in the second-level cache. If not, Hibernate will issue additional select:s against the database to load the missing entities.

By the way... if you are sure that most entities are already in the second-level cache it may be even faster to simply iterate over the list and use Session.get() or Session.load() for each id. Session.load() is probably very fast if you have enabled proxies for MyClass since it will simply create a new proxy for it (never hitting the database). Which method that works out the best probably also depends on what you are going to do with the objects later on. For example, if you expect the proxies to become initialized or not.[/quote]

Ideally, there should be a session.getObjects(classzz, idList) method. This method should figure out what's in the cache or not and builts another idList to hit the database to fetch all entities not in the cache with a single select. This leads me to my original question: why isn't there a session.getObjects?


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.