I have a table called Master_Info_tbl . His lookup table:
Here is the table code:
@Entity @Table(name="MASTER_INFO_T") public class CodeValue implements java.io.Serializable { private static final long serialVersionUID = -3732397626260983394L; private Integer objectid; private String codetype; private String code; private String shortdesc; private String longdesc; private Integer dptid; private Integer sequen; private Timestamp begindate; private Timestamp enddate; private String username; private Timestamp rowlastchange;
I have a service level that calls a method
service.findbycodeType ("Code1");
in the same way, this table is requested for other types of code, as well as for example code2, code3, etc. to code 10 , which gets the result set from the same table and appears in the jsp page dropdown list, since these dropdown pages are on 90% of the pages that I believe cache them around the world,
Any idea how to achieve this?
FYI: I use JPA and Hibernate with Struts2 and Spring. The database used is DB2 UDB8.2
@Pascal
Thanks so much for all your answers. It really helped me. I implemented everything that I had to implement (I think). However, I do not know if second level caching works or not. Since I canβt see anything in the log4j logging from the cache, and also nothing is displayed in the console. To show that the implementation of the second level cache works, I need to have some kind of confirmation and show it to my manager. So I'm kinda stuck.
Please, help!
I know that I am very close to finish it, but just ....
Here is my code (if you think something is missing or something should not be there, please let me know):
Entity class
@Entity @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Table(name = "CODEVALUE_T") public class CodeValue implements java.io.Serializable { //all the field name with getter and setter }
persistence.xml:
<properties> <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" /> </properties>
DAO Service Level:
try { System.out.println("Calling the findByCodetype() method"); final String queryString = "select model from CodeValue model where model.codetype" + "= :propertyValue" + " order by model.code"; Query query = em.createQuery(queryString); query.setHint("org.hibernate.cacheable", true); query.setParameter("propertyValue", propertyName); return query.getResultList(); } catch (RuntimeException re) { logger.error("findByCodetype failed: ", re); throw re; }
Log4j.property value to display verbose
log4j.category.org.hibernate.cache = DEBUG
ehcache.xml (this place is in the src / folder where my struts.xml file is located)
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="6000" timeToLiveSeconds="12000" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache>