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.