-->
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.  [ 8 posts ] 
Author Message
 Post subject: Second level cache access
PostPosted: Tue Feb 14, 2006 8:23 pm 
Newbie

Joined: Tue Feb 14, 2006 7:39 pm
Posts: 5
I am working with a legacy database and had problems searching for an object. When user enters code say "250" to search for an object database might store this id in several ways: "0000250" or "250 ". It depends what other legacy application was used to enter the data. So I needed a way to find out if object already loaded in second level cache without hitting the database. I want to check if object with id "0000250" is in cache, if not then check id "250 " and only after that call load method. I created this function:

Code:
protected boolean isObjectInCache(Class clazz, String id){


     SessionFactoryImplementor sessionFactoryImplementor =(SessionFactoryImplementor)getSessionFactory();

     if(logger.isDebugEnabled()){

          logger.debug("Checking if object is in cache: Class="+ clazz+", id="+id);
     }
     Cache cache = sessionFactoryImplementor.getSecondLevelCacheRegio n(clazz.getName());
     if(cache==null){

          return false;
     }

     CacheKey cacheKey = new CacheKey(id, Hibernate.STRING, clazz.getName(),
          EntityMode.POJO, sessionFactoryImplementor);
     CacheEntry cacheEntry = (CacheEntry)cache.get(cacheKey);
     if(cacheEntry==null){

          return false;
     }
     return true;

}



Is there an easier way to verify that without going into internals of hibernate and second level cache? Someone suggested to implement my own cache provider but then I would also need to implement my own dataaccess. I am looking for few line of code, not a whole project to do it.


Top
 Profile  
 
 Post subject: Re: Second level cache access
PostPosted: Wed Feb 15, 2006 1:53 pm 
Newbie

Joined: Wed Feb 15, 2006 9:22 am
Posts: 7
Location: Florham Park, NJ
Ilativ;

Perhaps you can create an additonal synthetic attribute in your mapping using a formula to trim the leading zeroes from the value. In this way, that lookup attribute will never have leading zeroes, even if the true, unmodified attribute does. I am not sure if your legacy database would support this, but for clarity, here's an example of what I mean in Oracle:

Code:
       <property name="funnyNumber" type="string">
            <column name="FUNNY_NUMBER" not-null="true" />
        </property>

        <property name="formatedNumber" formula="TO_NUMBER(FUNNY_NUMBER)" />


Now you lookup in the cache by the formatedNumber attribute.

//Nicholas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 2:59 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
Seems to me that implementing a custom Type via the UserType interface is what you want to do. This way you can coerce the values to and from the database. It would always be with "correct" value in hibernate.


Top
 Profile  
 
 Post subject: Re: Second level cache access
PostPosted: Thu Feb 16, 2006 12:21 am 
Newbie

Joined: Tue Feb 14, 2006 7:39 pm
Posts: 5
How I can look up in cache using non id attribute? I thought only calling load or get with id will try to access cache. I am not talking about query cache.

nickman wrote:
Ilativ;

Perhaps you can create an additonal synthetic attribute in your mapping using a formula to trim the leading zeroes from the value. In this way, that lookup attribute will never have leading zeroes, even if the true, unmodified attribute does. I am not sure if your legacy database would support this, but for clarity, here's an example of what I mean in Oracle:

Code:
       <property name="funnyNumber" type="string">
            <column name="FUNNY_NUMBER" not-null="true" />
        </property>

        <property name="formatedNumber" formula="TO_NUMBER(FUNNY_NUMBER)" />


Now you lookup in the cache by the formatedNumber attribute.

//Nicholas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 16, 2006 12:23 am 
Newbie

Joined: Tue Feb 14, 2006 7:39 pm
Posts: 5
I want to coerce not to access database

jt_1000 wrote:
Seems to me that implementing a custom Type via the UserType interface is what you want to do. This way you can coerce the values to and from the database. It would always be with "correct" value in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 16, 2006 12:50 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
ilativ wrote:
I want to coerce not to access database


that is exactly what it is meant for... take a look at the UserType interface, and you'll see that it provides you the hooks right before it sends to the database and right after it retrieves from DB, via the preparedstatement and resultset, respectively.

-JT


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 18, 2006 12:38 am 
Newbie

Joined: Tue Feb 14, 2006 7:39 pm
Posts: 5
jt_1000 wrote:
ilativ wrote:
I want to coerce not to access database


that is exactly what it is meant for... take a look at the UserType interface, and you'll see that it provides you the hooks right before it sends to the database and right after it retrieves from DB, via the preparedstatement and resultset, respectively.

-JT


Thanks. I tried that approach but it did not worked as I wanted. When I run ordinate queries it works, but when I do session.load(id) the hooks do not get invoked for the id. For instance if I search by name then id "000000250" is converted inside my hook to "250". But when I use session.load(clazz, "000000250") the hook that deal with resultset was not invoked. Maybe someone in hibernate team can take a look at this bug. Maybe they did it on purpose so user will not corrupt primary key. But then why it works for all other queries. They should still allow developers to overwrite that behavoir.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 1:32 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
why aren't you invoking the load with the coerced ID? meaning use "250"...instead of "00000250"? After all, I assume your UserType is converting the data read from the DB to 250 as it is loaded into hib layer...right? and when you save data (via hibernate), you'd convert the "000000250" to "250"..right? The idea is to standardize your representation in hibernate, even though the db may have different from...right? If this doesn't help to clear up..then post your UserType code, and we'll have a look.

-JT


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