Just started learning hibernate, and to understand hibernate 2nd level cache. I created a comment Entity and try to work on it. Here are my codes!
@Entity
@Table(name = "comment")
@FilterDefs(value={@FilterDef(name="projectFilter",parameters=@ParamDef(name="projectID", type="java.lang.Long" )), @FilterDef(name="issueFilter", parameters=@ParamDef( name="issueID", type="java.lang.Long" ) )})
@Filters(value={@Filter(name = "projectFilter", condition = "project_id = :projectID"),
@Filter(name = "issueFilter", condition = "issue_id = :issueID")})
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="comment")
public class Comment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, precision = 5, scale = 0 )
private long id;
@Column(name = "project_id", nullable = false)
private long projectId;
@Column(name = "issue_id", nullable = false)
private long issueId;
@Column(name = "author_id", nullable = false)
private long auhorId;
@Column(name = "author_name", nullable = false)
private String authorName;
@Column(name = "comment")
private String comment;
@Column(name = "created_date")
private Date createdDate;
I want to fetch user comment based on project or project and issue. In DAO,I have written the following function for comments based on projectid.
@Autowired
private SessionFactory sessionFactory;
...
@Override
public List<Comment> getAllProjectComments(long projectId) {
Session session = getCurrentSession();
Filter filter = session.enableFilter("projectFilter");
filter.setParameter("projectID", projectId);
return session.createQuery("from Comment").setCacheable(true).list();
}
and ehcahe.xml is below
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/hibernate-cache"/>
<defaultCache maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="1200"
timeToLiveSeconds="2400"
overflowToDisk="false"
maxElementsOnDisk="1000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1200"
memoryStoreEvictionPolicy="LRU"/>
<cache name="org.trackMyProject.entity.Comment"
maxElementsInMemory="50"
maxElementsOnDisk="500"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
I have added 500 comments in table for 2 projectids.
when the controller call DAO method for getting the comments based on the project id and subsequent call for that project id seems to work perfectly with 2nd level cache and DB is not getting hit. But if I switch between 2 projectIDs constantly, then each time DB gets hit, that I don't want.
Can anybody tell me what kind of mistake I have made or need to do more configuration.
Thanks in advance!!
EDIT!
classpath:hibernate.cfg.xml
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>