簡體   English   中英

org.hibernate.HibernateException: getNamedQuery 在沒有活動事務的情況下無效

[英]org.hibernate.HibernateException: getNamedQuery is not valid without active transaction

我有一個例外:

org.hibernate.HibernateException: getNamedQuery is not valid without active transaction    org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:340)
    $Proxy10.getNamedQuery(Unknown Source)

這是我的配置:

    ...
    <context:annotation-driven/>
    <beans:bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>       
    <tx:annotation-driven transaction-manager="transactionManager"/>
    ...

另外,我添加了context:annotation-driven因為tr:annotation-driven不起作用, <tx:annotation-driven/>使用從 Hibernate 獲取自己會話的transactionManager

我在帶注釋的事務中使用了 Hibernate3 的派生sessionFactory ,那么我該如何配置 Spring 呢?

事務管理器依賴於它用來管理事務的會話工廠。

通過添加<tx:annotation-driven />你告訴 Spring 事務是如何划分的。 在這種情況下,您可以使用注釋。

請參閱文檔頁面如何使用基於 XML 架構的配置

 <?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 definitions here --> </beans>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM