1

I got an exception:

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

Here is my configuration:

    ...
    <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"/>
    ...

Also, I added context:annotation-driven since the tr:annotation-driven is not working, does <tx:annotation-driven/> use the transactionManager which obtain its own session from Hibernate?

I used my derived sessionFactory using Hibernate3 inside the annotated transaction, so how do I configure the Spring to do so?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ann W.
  • 27
  • 1
  • 7

1 Answers1

0

The transaction manager has a dependency on session factory which it's using to manage transactions.

By adding <tx:annotation-driven /> you tell Spring how transactions are demarcated. In this case you can use annotations.

See the docs page how to use XML Schema-based configuration:

<?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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It's not working for transactions tx, so I added context element and mapping, as well as context:annotation-driven since I got session from hibernate directly, but no use, still no transaction. – Ann W. Apr 27 '16 at 06:10
  • Update Hibernate libraries to at least Hibernate 4, make sure it's working before using transactions. Also you need `@Transactional` on repositories to demarcate your transactions. – Roman C May 06 '16 at 09:44