【问题标题】:Hibernate - save is not valid without active transaction休眠 - 没有活动事务,保存无效
【发布时间】:2014-08-14 15:48:43
【问题描述】:

我使用 @Transactional 而不是创建 TransactionManager 对象并调用。有人可以给出我收到此 HibernateException: save is not valid without active transaction 的原因吗?

Bean 用于 sessionFactory 和 TransactionManager

<bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
</bean>
<bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
</bean>

UserServiceImpl.java

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public void addUser(User user) {
        userDao.addUser(user);
    }
}

UserDaoImpl.java

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addUser(User user) {
        sessionFactory.getCurrentSession().save(user);
    }
}

堆栈跟踪

org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at com.sun.proxy.$Proxy27.save(Unknown Source)
at com.hemanths.expense.manager.hibernate.dao.impl.UserDaoImpl.addUser(UserDaoImpl.java:19)
at com.hemanths.expense.manager.service.impl.UserServiceImpl.addUser(UserServiceImpl.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

休眠.hbm.xml

<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/emanager</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>

        <mapping class="com.hemanths.expense.manager.hibernate.entity.User"/>
    </session-factory>

</hibernate-configuration>

【问题讨论】:

  • 您是否通过将&lt;tx:annotation-driven/&gt; 放入应用程序上下文来激活@Transactional 注释?
  • 你的 spring 配置中有 吗?
  • 我正在运行测试。我需要注释测试吗?像 @ContextConfiguration 在春天的情况下?
  • 你是否在 HibernateTransactionManager 中设置了断点来检查你的程序是否正在进入事务性 AOP?
  • 你的 spring 配置 xml 中有 thread 吗?如果是这样,请将其删除。

标签: java spring hibernate


【解决方案1】:

删除

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

试试

【讨论】:

猜你喜欢
  • 2018-12-31
  • 1970-01-01
  • 2016-05-19
  • 2015-10-19
  • 2014-08-13
  • 2012-08-13
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多