如果没有活动事务,Spring Hibernate集成抛出getFlushMode无效

我正在研究示例Spring Hibernate示例,遵循一个教程并且遇到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 

这是我的代码:

Person.java – 简单的POJO

 public class Person { private Integer id; private String name; private String email; // Setters & Getters } 

person.hbm.xml – 映射文件

            

PersonDao.java – 我的DAO类

 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 – 服务层

 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 fetchAllPersons() { return getPersonDao().selectAll(); } } 

spring-config.xml – Spring配置文件:

                com/examples/model/person.hbm.xml      org.hibernate.dialect.MySQLDialect thread           

最后我的主程序 – 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 persons = personService.fetchAllPersons(); System.out.println("The list of all persons = " + persons); System.out.println("************** ENDING PROGRAM *****************"); } } 

当我运行程序时,我遇到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 

我遵循了这个SOpost中给出的解决方案 – Spring / Hibernate Exception:如果没有活动事务,createCriteria无效

然后我得到一个例外说:

 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. 

请帮我解决这个问题?

如错误所示,您必须使用事务,因此在spring配置文件中声明事务管理器:

              

这里我们还需要使用aoptx命名空间,因此更新beans的命名空间声明如下:

  

最后删除配置文件中的以下行:

 thread