I'm trying to access the current hibernate session in a test case, and getting the following error:

org.hibernate.HibernateException: No
Hibernate Session bound to thread, and
configuration does not allow creation
of non-transactional one here at
org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at
org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)

I've clearly missed some sort of setup, but not sure what.

Any help would be greatly appreciated. This is my first crack at Hibernate / Spring etc, and the learning curve is certainly steep!

Ideally you should have @Transactional(propagation = Propagation.REQUIRED) if you are testing some dao/repository tests which are themselves not annotated with transactions since they depend upon the class which initiated to be in transaction. On @TransactionConfiguration tell it about your transaction manager @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
–
vsinghMay 17 '12 at 17:53

I just read your post and I managed to overcome the problem for me. Now I don't see new lines in tables though while no error is thrown. I assume I have some how to stop rollback?
–
hephestosJun 25 '12 at 7:50

in your method and class to test. Session management is done by the Hibernate / Spring /JUnit test configuration, as later is done in the Hibernate / Spring configuration in the real application.

This is how it worked for my tests. In the final web application there will automatically be a Hibernate session associated with the current web request and therefore in testing there should be no sessionFactory.openSession() call.

actually, this is totally wrong. You want to call getCurrentSession() otherwise you will be responsible for closing it and your transactions won't work with the annotation driven model.
–
JustinMay 21 '10 at 17:07

@Justin - Thanks, the post has been updated based on this feedback. @ Downvoter - care to comment why? (This post has been updated for over a year!)
–
Marty PittMay 21 '12 at 7:49