I am working on a sample Spring Hibernate example by following a tutorial and got stuck with the exception saying
Exception in thread "main"
org.springframework.orm.hibernate4.HibernateSystemException: getFlushMode
is not valid without active transaction; nested exception is
org.hibernate.HibernateException: getFlushMode is not valid without active
transaction
Here is my code:
Person.java - Simple POJO
public class Person {
private Integer id;
private String name;
private String email;
// Setters & Getters
}
person.hbm.xml - Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.examples.model.Person"
table="PERSON">
<id column="ID" name="id">
<generator class="increment" />
</id>
<property name="name" column="NAME" />
<property name="email" column="EMAIL" />
</class>
</hibernate-mapping>
PersonDao.java - My DAO Class
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import com.examples.model.Person;
public class PersonDao extends HibernateDaoSupport {
public void insert(Person person) {
getHibernateTemplate().save(person);
}
public List selectAll() {
DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
return getHibernateTemplate().findByCriteria(criteria);
}
}
PersonService.java - Service layer
public class PersonService {
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void addPerson(Person person) {
getPersonDao().insert(person);
}
public List<Person> fetchAllPersons() {
return getPersonDao().selectAll();
}
}
spring-config.xml - Spring Configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="personService"
class="com.examples.service.PersonService">
<property name="personDao" ref="personDao" />
</bean>
<bean id="personDao"
class="com.examples.dao.PersonDao">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/examples/model/person.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/hib" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
</beans>
Finally my main program - MainApp.java
public class MainApp {
public static void main(String[] args) {
System.out.println("************** BEGINNING PROGRAM **************");
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-config.xml");
PersonService personService = (PersonService) context
.getBean("personService");
Person person = new Person();
person.setName("Robin");
person.setEmail("robin@gmail.com");
personService.addPerson(person);
System.out.println("Person : " + person + " added successfully");
List<Person> persons = personService.fetchAllPersons();
System.out.println("The list of all persons = " + persons);
System.out.println("************** ENDING PROGRAM *****************");
}
}
When I run the program I am getting below exception:
Exception in thread "main" org.springframework.orm.hibernate4.HibernateSystemException: getFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getFlushMode is not valid without active transaction
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:216)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:343)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at com.examples.dao.PersonDao.insert(PersonDao.java:13)
at com.examples.service.PersonService.addPerson(PersonService.java:21)
at com.examples.MainApp.main(MainApp.java:24)
Caused by: org.hibernate.HibernateException: getFlushMode is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy6.getFlushMode(Unknown Source)
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1134)
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)
... 5 more
I followed the solution given in this SO post - Spring/Hibernate Exception: createCriteria is not valid without active transaction
then I got an exception saying:
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.
Please help me how to fix this issue?