1. Problem Description
Spring 3.2.6
Hibernate 4.2.0 org.hibernate.HibernateException No session found for current thread
2. Analysis:
Quoted from R1:getcurrentsession () only makes sense inside a scope of transaction. Need to declare a appropriate transaction manager, demarcate boundaries of transaction and perform data access inside It.
3. Solution:
3.1 ADD configuration in application context.
<aop:aspectj-autoproxy/>
<bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager ">
<property name=" sessionfactory "ref=" Sessionfactory "/>
</bean>
<tx:annotation-driven transaction-manager=" TransactionManager " >
3.2 Add Spring ' s @Transactional annotation
@Autowired
private sessionfactory sessionfactory;
/**
* @return Session Object
*
/private Session currentsession () {return
Sessionfactory.getcurrentsession ();
}
/**
* @param operator * *
@Transactional public
void Saveoperator (operator operator) {
Currentsession (). Save (operator);
}
References:
1:http://stackoverflow.com/questions/10459922/org-hibernate-hibernateexception-no-session-found-for-current-thread
2:http://www.iteye.com/problems/94825