Hibernate5,Spring 4 - org.hibernate.HibernateException:无法获取当前线程的事务同步会话

时间:2016-07-12 06:48:34

标签: spring hibernate

我们正在将项目从Hibernate 3.6.10升级到Hibernate 5.2.0,但在迁移时会出错。我在StackOverFlow和Google上发过很多帖子,但没有找到任何解决方案。

考虑代码参考this website。我只将数据库连接到 PostgreSQL 数据库。

如果我在添加这些JAR(ALL Added JAR files screenshot)之后运行此示例,除了hibernate JAR是3.6.10-final,它完全正常。但是,如果我为hibernate 5升级进行了这些更改:

  1. 更改添加为Hibernate-core-5.2.1-Final.jar
  2. 的JAR文件
  3. 将hibernate3文件的引用更改为hibernate5(3个替换;在EmployeeDao导入中为1,在applicationContext.xml中为2)
  4. 它会抛出以下错误:

    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.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1132)
    at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:618)
    at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:615)
    at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:340)
    at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:307)
    at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:615)
    at hibernate.test.EmployeeDao.saveEmployee(EmployeeDao.java:12)
    at hibernate.test.InsertTest.main(InsertTest.java:21)
    

    我也做了这些修改,但得到了同样的错误: 1.在EmployeeDao中评论:

    /*HibernateTemplate template;  
    
    public void setTemplate(HibernateTemplate template) {  
        this.template = template;  
    }  */
    

    并扩展HibernateDaoSupport ,以便它提供sessionFactory setter。

    将以下代码替换为:

    public void saveEmployee(Employee e){  
        template.save(e);  
    } 
    

    这样:

    public void saveEmployee(Employee e){  
          this.getHibernateTemplate().save(e);
    }  
    

    经过调试后我发现在Spring-orm-4.3.1提供的HibernateTemplate类中,Jar抛出了上述错误。

    Here is the snapshot.

    任何人都可以帮我解决这个问题:哦。我们已经坚持了很久。我很感激帮助。

1 个答案:

答案 0 :(得分:0)

创建DataSource.xml

<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:properties/database.properties</value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

使用下面给出的单独的hibernate.xml文件创建sessionFactory

<?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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">false</prop>
         <prop key="hibernate.hbm2ddl.auto">update</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>com.rapidtech.rapidtechorganic.model.Lot</value>
        <value>com.rapidtech.rapidtechorganic.model.ProductAvailable</value>
        <value>com.rapidtech.rapidtechorganic.model.Client</value>
        <value>com.rapidtech.rapidtechorganic.model.Invoice</value>
        <value>com.rapidtech.rapidtechorganic.model.ProductBuyed</value>
    </list>
    </property>

</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

在servel-context.xml中包含xml文件

<!-- Database Configuration -->
<beans:import resource="classpath:database/DataSource.xml"/>
<beans:import resource="classpath:database/Hibernate.xml"/>

然后在您的Abstract类中注入sessionFactory

@Resource
private SessionFactory sessionFactory;
public void save(Entity entity){
   Session session = sessionFactory.openSession();
   session.beginTransaction();
   session.save(entity);
   session.getTransaction().commit();
   session.close();
}