org.hibernate.HibernateException:如果没有活动事务,save无效

时间:2014-06-09 06:27:22

标签: spring hibernate transactions save

我在使用Spring的hibernate保存数据时遇到了问题。 当我保存数据时出错:

Exception in thread "main" org.hibernate.HibernateException: save is not valid   without active transaction

https://gist.github.com/RuzievBakhtiyor/f3009dbc6a9c31090b59

Spring beans config:

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

<import resource="hibernate.xml"/>
<import resource="dataSource.xml"/>

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





<context:component-scan base-package="neron" />

Hibernate配置bean:

<?xml version="1.0" encoding="UTF-8"?>
<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="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="neron.models" />

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.show_sql">true</prop>

        </props>
    </property>
</bean>

DataSource配置bean:

<?xml version="1.0" encoding="UTF-8"?>
<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 name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <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="123456789" />
</bean>
</beans>

TestDao:

package neron.dao.Impl;

import neron.dao.TestDao;
import neron.models.Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;


@Transactional
@Repository("testDao")
public class TestDaoImpl implements TestDao {


SessionFactory sessionFactory;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

private Session getCurrentSession()
{

    return (Session)sessionFactory.getCurrentSession();

}

public void save(Test test) {
    getCurrentSession().save(test);
}

public Test findById(int id) {
    return (Test) getCurrentSession().get(Test.class,id);
}

}

2 个答案:

答案 0 :(得分:3)

您需要在事务边界内执行数据库更新操作(插入,更新,删除)。

Session session = getCurrentSession();
Transaction trans = session.beginTransaction(); //begin transaction
//db operation 
session.save(test)
trans.commit();  //end transaction

OR 对于注释支持,在spring config bean中添加此

<tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true" />

答案 1 :(得分:0)

从sessionFactory-> hibernateProperties删除该标签

<property name="hibernate.current_session_context_class">thread</property>