org.hibernate.HibernateException: No Session found for current thread
getCurrentSession()
only makes sense inside a scope of transaction.
You need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows:
<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /></bean>
.
PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);TransactionTemplate tx = new TransactionTemplate(ptm);tx.execute(new TransactionCallbackWithoutResult() { public void doInTransactionWithoutResult(TransactionStatus status) { // Perform data access here }});
See also:
I came across same problem and got solved as belowAdded @Transactional on daoImpl class
Added trnsaction manager in configuration file:
<tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean>
I'll just add something that took me some time to debug : don't forget that a @Transactional annotation will only work on "public" methods.
I put some @Transactional on "protected" ones and got this error.
Hope it helps :)
http://docs.spring.io/spring/docs/3.1.0.M2/spring-framework-reference/html/transaction.html
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.