Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将spring与hibernate会话和事务管理集成?_Hibernate_Spring_Transactions - Fatal编程技术网

如何将spring与hibernate会话和事务管理集成?

如何将spring与hibernate会话和事务管理集成?,hibernate,spring,transactions,Hibernate,Spring,Transactions,我是冬眠和春天的初学者。我已经了解了hibernate事务的划分(至少我这么认为)。但是在编写了一些类似这样的方法之后: sessionFactory.getCurrentSession().beginTransaction(); //do something here sessionFactory.getCurrentSession().endTransaction(); 我开始想要避免它,并且想要在我的方法之外自动完成它,这样我就只写“//在这里做点什么”部分。我读过Transaction

我是冬眠和春天的初学者。我已经了解了hibernate事务的划分(至少我这么认为)。但是在编写了一些类似这样的方法之后:

sessionFactory.getCurrentSession().beginTransaction();
//do something here
sessionFactory.getCurrentSession().endTransaction();
我开始想要避免它,并且想要在我的方法之外自动完成它,这样我就只写“//在这里做点什么”部分。我读过TransactionProxyFactoryBean,认为xml配置非常长,必须对我想要进行事务处理的每个类重复进行配置,因此如果可能,我希望避免使用它

我尝试使用@Transactional,但根本不起作用。我的applicationContext.xml中有这些行

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager" />
在这种情况下,确切的错误消息是:“org.hibernate.HibernateException:save在没有活动事务的情况下无效”

更新:我尝试从外部单元测试(即从实际的web应用程序)调用userService.addUser()方法,但也遇到了相同的错误

这是我的hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- all my mapping resources here -->
    </session-factory>
</hibernate-configuration>

1.
org.hibernate.dialogue.mysqldialogue
线
org.hibernate.cache.NoCacheProvider
真的
更新
userService类标记为@Transactional。我正在使用Hibernate3.3.2和Spring2.5.6


我可以就如何修复此问题提供一些建议吗?

删除以下行,它会干扰Spring管理的事务:

<property name="current_session_context_class">thread</property> 
线程

@hephestos:current_session_context_class参数的值决定会话(Hibernate会话)必须绑定到的上下文

默认情况下,它与当前运行的线程绑定。但是,当使用“jta”管理事务时,将此参数的值更改为“jta”会将会话绑定到当前jta事务上下文


基本上,它定义了会话的上下文。更多信息:

显示您的
hibernate.cfg.xml
。我也有类似的问题。但是当我删除这一行时,出现了以下错误。HibernateeException:未配置CurrentSessionContext!org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)您好,为什么删除这一行会有帮助?我不明白,我有一个类似的问题,尝试替换值也没有帮助。如果删除该行,默认值是多少?有人知道吗?@hephestos:如果你不指定这个属性,Spring会为它提供自己的值,以便将它的事务管理抽象与Hibernate集成,否则Spring不会设置它。@axavt,这是我理解的,但默认值是什么?因为如果我用这三个值覆盖它,似乎没有一个与默认值匹配。同样从谷歌,我必须指出,由于配置的不同,我没有对手。这就是为什么要问这里
<property name="current_session_context_class">thread</property>