Hibernate4 integration with Spring3, if Getcurrentsession is used where the session was obtained, a mistake may be reported: "No Session found for the current thread", the cause of this error, online There are many solutions, but the specific reasons for the analysis, but not much, here reproduced a principle analysis:
Sessionfactory's getcurrentsession does not guarantee that a new one will automatically be created without the current session, depending on the implementation of the Currentsessioncontext. Sessionfactory will call Currentsessioncontext's Currentsession () method to get the session. In spring, if we call getcurrentsession () directly without configuring TransactionManager and not calling Sessionfactory.opensession () in advance, the program throws "No Session found for current thread "exception. If you configure a transaction boundary that is Tranactionmanager and configured by @transactional or declaratively, Spring creates a session for the current thread through AOP before starting the transaction. Call Getcurrentsession () to get the correct result.
However, the reason for the above exception is that spring provides its own currentsessioncontext implementation, if we do not intend to use spring, Instead, I created sessionfactory directly from Hibernate.cfg.xml, and for the Hibernate.cfg.xml
The Current_session_context_class is set to thread, or threadlocalsessioncontext is used, then when we call Getcurrentsession (), If no session exists for the current thread, a binding to the current thread is created.
Hibernate uses jtasessioncontext,spring to provide its own springsessioncontext by default, so we do not have to configure Current_session_context_class, When hibernate is integrated with spring, the sessioncontext is used, so the effect of calling Getcurrentsession () is entirely dependent on the implementation of Springsessioncontext.
In the absence of spring, Hibernate is used, and if no Current_session_context_class is configured in Hibernate.cfg.xml, there is no JTA, then the program throws "No Currentsessioncontext configured! " Abnormal. The workaround at this point is to configure Current_session_context_class as thread in Hibernate.cfg.xml.
Using Hibernate in spring, if we configure TransactionManager, then we should not call Sessionfactory's opensession () to get Sessioin, Because this session is not managed by the transaction.
As for the solution, you can use the following methods:
1. In the spring configuration file, add
Program code
<tx:annotation-driven transaction-manager= "TransactionManager"/>
and use annotations on classes that handle business logic
Program code
@Service
public class Customerserviceimpl implements CustomerService {
@Transactional
public void Savecustomer (customer customer) {
Customerdaoimpl.savecustomer (customer);
}
...
}
In addition, in the Hibernate configuration file, you can also add such a configuration to avoid this error:
Program code
<property name= "Current_session_context_class" >thread</property>
Hibernate4 No Session found for current thread