I\'ve been having an annoying exception a lot lately, and after some research on Google and this forum I still haven\'t found an answer that could solve my problem.
Add @Transactional
above you database operation methods
Make sure you have annotation driven transation manager add <tx:annotation-driven/>
above HibernateTransactionManager
configuration in applicationContext.xml
file
You must forgot to add @Transactional annotation to your DAO service class/method, this will specify that your database operation will be handled by spring managed transaction.
I just stumbled onto this too. I needed to change the flush mode in Spring's OpenSessionInViewFilter
to manual, and suddenly I started getting this exception. I found out that the problems were happening in methods that were not annotated as @Transactional
, so I guess that Spring implicitly treats all data access code outside such methods as read only. Annotating the method solved the problem.
Another way is to call the setCheckWriteOperations method on the HibernateTemplate
object.
Try to use this
this.getHibernateTemplate().setCheckWriteOperations(false);
Still if you are using hibernate4 and not able to update entity/table then you need to create session and need to do flush the session i.e.
this.getHibernateTemplate().setCheckWriteOperations(false);
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
session.saveOrUpdate(entityName,app);
session.flush();
session.close();
I changed the single session propery from view filter. Problem solved:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
Below piece of code is worked for me.
hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setCheckWriteOperations(false);