无法让@Transactional和Hibernate 4一起工作

时间:2013-01-29 16:54:32

标签: hibernate spring-transactions

我正在努力让Hibernate 4和Spring Transactions一起玩。在这一点上,我只是想尝试一个有效的Hibernate设置。

这是调用的方法。当我打电话给我时,我得到

"org.hibernate.HibernateException: createQuery is not valid without active transaction"



@Autowired
SessionFactory sessionFactory;

@Transactional
public SecurityUserDO findSecurityUser(String email)
{
    Session session = sessionFactory.getCurrentSession();

    Query query = session.createQuery( "from SecurityUserDO where email = :email" );
    query.setString( "email", email );
    return (SecurityUserDO) query.uniqueResult();
}

这是配置:

<!-- use annotations to find beans in this package -->
<context:annotation-config/>
<context:component-scan base-package="com.jelli.phoenix.model" />

<!--  Default session factory. Requires a dataSource bean to be defined in another config.
This works with a embedded data source for integration test setup. For real deployment
the datasource should be a c3p0 pool. -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="byName"
     depends-on="dataSource">
    <property name="configLocation">    
        <value>
            classpath:hibernate.cfg.xml
        </value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.current_session_context_class">thread</prop>

            <!-- Database connection settings --> 
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

            <!-- Caching -->
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>

            <!-- Performance Settings --> 
            <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
            <prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
            <prop key="hibernate.max_fetch_depth">1</prop>

            <!-- When SQL is logged, pretty-print it --> 
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

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

<!-- Use @Transaction annotations for managing transactions  -->    
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

数据源在另一个文件中指定。

我已经阅读了我能找到的每一篇文章。有几个人说要删除

"&lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;", 

但这导致getCurrentSession()失败。

1 个答案:

答案 0 :(得分:2)

回答我自己的问题:

正如许多其他地方所述,问题在于我:

&lt; prop key =“hibernate.current_session_context_class”&gt; thread&lt; / prop&gt;

这会禁用Spring Transactions,但会产生一个问题,即SessionFactory.getCurrentSession()不再起作用。我的解决方法是编写一个调用SessionFactory.openSession()的实用程序类,并将该会话保存在ThreadLocal中。

我不得不说我真的不明白为什么Spring Transactions拒绝使用Hibernate的基于线程的会话上下文。