-->
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.  [ 2 posts ] 
Author Message
 Post subject: L2 Cache Not Caching Non-mapped Properties?
PostPosted: Fri Mar 17, 2006 9:14 am 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
Hibernate version: 3.1.2

Does the L2 cache hold the values of extended properties that are not mapped to the database by hibernate?

For example, this POJO:

Code:
public class Account implements java.io.Serializable {
     //Mapped to database via hibernate
     private int accountid;
     private int name;

     //Not mapped to database... just properties
     private boolean flag;
     private int expensiveToCalculate;

     //Method called by event listener
     public void load(){
          if (!flag){
               //Set the flag to true so expensive work only done once
               flag=true;
               //Calculate stuff
               expensiveToCalculate = someOtherExpensiveMethod();
          }
     }
}


The load() method is called from a PostLoadEventListener. It works properly the first time the object is loaded... the expensive calculation is done and the flag is set so that I can use the object from the cache without doing the expensive calculation again.

However, the next time I access the Account object it calls load(), which is fine, but it does the expensive calculation a second time... because the value of boolean flag is apparently not held in cache... or is re-initialized to its default value each time the Account object is pulled from the cache. Is this correct?

I know that hibernate is pulling the object from cache because I'm outputting sql and am not seeing any additional sql calls. I have the object cached and the query cached.

It appears that the L2 cache only caches values that are mapped by hibernate, not other properties that I add to the POJO and not the mapping.

Thanks for any insight.

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 9:46 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Yep, Hibernate 2nd level cache only stores persistent properties/associations.
It disassembles your entity object into serializable persistent objects or associations serializable primary keys.

So in your case it forgets about your boolean value being true.

Since when it pulls your entity from the cache, it creates new instance (there for your boolean value is by default false) of your entity and then assembles it from serializable properties.

_________________
--------------------------------
Ales Justin
JBoss, a division of Red Hat


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