Friday, August 1, 2008

Struts 2 and Hibernate gotcha

This post will be a short one, since struts2/hibernate has taken already too much of my time lately :( .

We have a web application set up like this example here . For some time now I have been wrestling with a problem that right after insert joined data for my Entity bean does not appear in my JSP. The same problem is documented in this thread here.

I've been actively searching for a solution to this problem for a few days now. As it turns out the culprit is Hibernate caching. You can find a small overview of Hibernate caching here . For us the simple solution was to turn off hibernate second level cache altogether. And now our applicationContext.xml has this in it:


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL">
<property name="showSql" value="true">
</property>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.cache.use_second_level_cache" value="false">
<entry key="hibernate.cache.use_query_cache" value="true">
<entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider">
<entry key="hibernate.show_sql" value="true">
<entry key="hibernate.use_sql_comments" value="false">
<entry key="hibernate.format_sql" value="false">
</entry></entry></entry></entry></entry></entry></map>
</property>
</bean></property></property></bean>

Now that was productive for a few days work - wasn't it? Extremely user friendly indeed!!!

No comments:

Post a Comment