-->
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: Spring HibernateTemplate and EHCache
PostPosted: Fri Dec 02, 2005 12:58 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 pm
Posts: 5
Location: DC
Hibernate version: current 3.something

I'm having problem with caching using Spring's HibernateTemplate

It appear that my application is caching the ContactInfo, but not the State

my hibernate.hbm.xml file:

Code:
<class name="mypackage.ContactInfo" table="CONTACT_INFO" schema="User_App" lazy="false">
    <cache usage="read-write" />
    <id name="id" column="ID" type="long">
        <generator class="sequence">
   <param name="sequence">ContactId_Sequence</param>
        </generator>
    </id>
      
    <property name="name" column="NAME" type="string"/>
    <property name="phone" column="PHONE" type="string"/>
    <property name="street" column="STREET" type="string"/>
    <property name="city" column="CITY" type="string"/>
    <many-to-one
       name="state"
       class="mypackage.State"
           column="STATE"
           property-ref="state"
    />
    <property name="zip" column="ZIP" type="string"/>
    <property name="activeFlag" column="ACTIVE_FLAG" type="integer"/>
</class>



Code:
<class name="mypackage.State" table="STATE" schema="User_App" lazy="false">
    <cache usage="read-only" />
    <id name="id" column="id" type="java.lang.Long"></id>

    <property name="abbrev" column="ABBREVIATION" type="string"/>
    <property name="fullname" column="FULLNAME" type="String" />
    <property name="code" column="STATE_CODE" type="short"/>
</class>



I set up my EHCache

Code:
<ehcache>
    <defaultCache
        maxElementsInMemory="500"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="false"
    />

    <cache name="mypackage.ContactInfo"
        maxElementsInMemory="200"
        eternal="true"
        overflowToDisk="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
    />
       
    <cache name="mypackage.State"
        maxElementsInMemory="60"
        eternal="true"
        overflowToDisk="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
    />
</ehcache>



my DAO

Code:
public List getContactInfoList(){
    return hibernateTemplate.find("from ContactInfo a where activeFlag = 1");
}


public ContactInfo getContactInfo(Long id){
    return (ContactInfo) hibernateTemplate.load(ContactInfo.class, id);
}

public List getStateList(){
    return hibernateTemplate.find("from State s order by s.abbrev");
}

public State getState(String stateAbbr){
    List list =  hibernateTemplate.find("from State s  where s.abbreviation=?  order by s.abbreviation",stateAbbr);
    return (list.size() > 0)? (State) list.get(0): null;
}



my code of calling the DAO methods. ContactManager just delegate the call to my DAO

Code:
List states = ContactManager.getStateList();
List contacts = ContactManager.getContactInfoList();

Long contactId = Long.parseLong(request.getParameter("contact_id"));
ContactInfo currContact = null;
if (contactId == null)
    currContact = (Contactinfo) list.get(0);
else
    currContact = ContactManager.getContactInfo(contactId);

// jsp code to display a combobox of contacts..with the currContact
// selected..and textfields to display the currContact info

when the user select a contact info from the combobox, the page send the add the contact id in the request parameter and submit the page (which call the same page again..with the contact info) .. user have option to add, update, delete contact info (which is not relevent here)


I log every call hibernate makes to the database (show_sql=true).
What i noticed is that hibernate will always call to fetch the ContactList
and it also call to fetch the State. I think caching is done using the primary key (using get() or load(0 method of the hibernateTemplate).

Is there a way for me to cache the ContactInfo List (getContactInfoList())and State (getState()) using Spring HibernateTemplate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
try removing the "property-ref"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:26 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 pm
Posts: 5
Location: DC
thanx steve,

but without th property-ref, i get this exception:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not execute query ... Fail to convert to internal representation;

the property-ref is needed to load the State object into the ContactInfo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Why do you think that? A "property-ref" is used to describe a relation based on somthing other than the associated entity's primary key.

Regardless, associations based on a "property-ref" will never be resolved in the second level cache. The data in the second level cache is only keyed by identifier...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 4:34 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 pm
Posts: 5
Location: DC
steve wrote:
Why do you think that? A "property-ref" is used to describe a relation based on somthing other than the associated entity's primary key.


i thought of that because of the Exception hibernate gave me. i'm new at this..so it was only a guess.

steve wrote:
The data in the second level cache is only keyed by identifier...


ah, so i guess i can do an EHCache on a collection without using some mechanism..like caching the Query instead ?
I've tried this and it appears it cache the collection in EHCache.

this is what i've done (which is also in my other post (sorry for "crossposting?"..i thought it was a different topic)

Code:
<ehcache>
    <cache name="query.ContactInfoList"
        maxElementsInMemory="200"
        eternal="true"
        overflowToDisk="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
    />
</ehcache>

public List getContactInfoList(){
    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("query.ContactInfoList");
    List list = hibernateTemplate.find("from AdoContactInfo a where active = 1");
    hibernateTemplate.setCacheQueries(false);
    return list;
}


What i don't like about this is that i have to specified the caching in the code, which i think is invasive. I would have to comment out the cahing code and the ehcache xml info if i decided not to cache the collection...but it seem i can't get around this.

Perhaps i should use an Aspect (using Spring) to set the caching?

p.s. thanx for your assistant


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.