1

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()しかし、それは失敗の原因になります。

4

1 に答える 1

2

私自身の質問に答える:

問題は、他の多くの場所で概説されているように、私が持っていたということです:

<prop key="hibernate.current_session_context_class">スレッド</prop>

これにより Spring トランザクションが無効になりますが、SessionFactory.getCurrentSession() が機能しなくなるという問題が発生します。私の回避策は、SessionFactory.openSession() を呼び出してセッションを ThreadLocal に保存するユーティリティ クラスを作成することです。

Spring Transactions が Hibernate のスレッドベースのセッション コンテキストの操作を拒否する理由が本当に理解できないと言わざるを得ません。

于 2013-01-30T22:33:43.620 に答える