Results 1 to 9 of 9

Thread: trouble with <tx:annotation-driven/> and hibernate

  1. #1
    Join Date
    Sep 2007
    Posts
    4

    Default trouble with <tx:annotation-driven/> and hibernate

    I'm new to Spring, but i've read a lot of documentation(even searched this forum for this topic) , but still can't figure this one out. need help from anyone with experience with @Transactional using <tx:annotation-driven>

    I have this:

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

    <tx:annotation-driven/>

    ...


    "sessionFactory" is correctly defined.
    Then my service class (Which uses Hibernate 3.1) has this:

    ...

    @Transactional(propagation=Propagation.REQUIRES_NE W, readOnly=true)
    public User getUserByUsername(String username) {
    return ((User) getSession()
    .createQuery("from User u where u.username like :un")
    .setString("un", username)
    .setMaxResults(1)
    .uniqueResult());
    }

    ...

    then this is from the stack trace:

    ...

    09:51:11,491 ERROR AutoHandler:30 - createQuery is not valid without active transaction
    org.hibernate.HibernateException: createQuery is not valid without active transaction
    at org.hibernate.context.ThreadLocalSessionContext$Tr ansactionProtectionWrapper.invoke(ThreadLocalSessi onContext.java:297)
    at $Proxy39.createQuery(Unknown Source)
    at net.ehosp.model.daoImpl.UserDAOHibernate.getUserBy Username(UserDAOHibernate.java:61)
    at net.ehosp.controller.LoginActionBean.login(LoginAc tionBean.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    Any ideas? is this a problem with hibernate?

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,854

    Default

    Your DAO instance is not being managed by Spring, and therefore the transactional proxy is not being applied. You should be getting the DAO instance from Spring (typically using dependency-injection into the calling object). How are you currently getting this object?

  3. #3
    Join Date
    Sep 2007
    Posts
    4

    Default

    Thanks. The DAO is being managed by Spring through dependency injection.

  4. #4

    Default

    How are you instantiating the spring context? A common mistake is to use of the BeanFactory implementations instead of a full-fledged ApplicationContext.

  5. #5

    Default

    I'm having the same problem. Have you found a solution?

  6. #6
    Join Date
    Sep 2007
    Posts
    4

    Default

    No I still haven't figured it out. I actually gave up for a while!

    In response to Plethora's question about how i'm instantiating Spring, well I'm using Stripes, so Spring is loaded by a Stripes interceptor (net.sourceforge.stripes.integration.spring.Spring Interceptor), which is configure in web.xml.

    I have a feeling it might have something to do with this, but i have no clue how. I'll post the question in the Stripes forum

  7. #7

    Default

    I found out my issue. I'm using the 2.1-m4 release. It appears to be a bug. I reverted to 2.0.7 and everything worked.

  8. #8
    Join Date
    Sep 2007
    Posts
    4

    Default

    please, what was your issue exactly, so i can see how to fix mine.

  9. #9

    Default

    That's almost all there was to it. I'm using maven. I switched my dependency from 2.1-m4 to 2.0.7 and spring-test (from the new version) back to spring-mock. Then everything worked.

    My setup was a DAO, xml configured sessionFactory, txManager, etc, and a junit test class extending AbstractTransactionalDataSourceSpringContextTests. Works with 2.0.7, not with 2.1-m4.

    If you are using the new annotation stuff from 2.1 - the reason I wanted to give it a try - you may have to wait for an official release.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •