I have been receiving the following error when trying to integrate spring with hibernate in a standalone application.
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at CarDAO.insert(CarDAO.java:38)
at CarTest.main(CarTest.java:19)
Bean File:
public class Car {
int carid;
String carname;
public int getCarid() {
return carid;
}
public void setCarid(int carid) {
this.carid = carid;
}
public String getCarname() {
return carname;
}
public void setCarname(String carname) {
this.carname = carname;
}
}
My hibernate mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Car" table="Car">
<id name="carid" column="carid"/>
<property name="carname" column="carname"/>
</class>
</hibernate-mapping>
CarDAO:
import javax.persistence.EntityManager;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
//import org.springframework.transaction.annotation.Transactional;
public class CarDAO {
HibernateTemplate template;
EntityManager entityManager;
TransactionTemplate template1;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
@Transactional
@Scope("session")
public void insert(Car c){
//template.getSessionFactory().getCurrentSession().setFlushMode(org.hibernate.FlushMode.AUTO);
//entityManager.setFlushMode(javax.persistence.FlushModeType.AUTO);
// session.setFlushMode(FlushMode.AUTO);
template.save(c);
entityManager.flush();
}
}
ApplicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="datasourceBean" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionfactoryBean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasourceBean"/>
<property name="mappingResources">
<list>
<value>Car.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionfactoryBean"></property>
<!-- <property name="flushModeName" value="FLUSH_COMMIT"/> -->
</bean>
<bean id="CarDAO" class="CarDAO">
<property name="template" ref="template"></property>
</bean>
</beans>
I have searched over the web for appropriate solution but was unable to find it.Please help thanks in advance