Hibernate4 integration with SPRING3, if you use getcurrentsession in place of the session, you may report a mistake: "No Session found for the current thread", the original Because, there are many solutions on the net, but the analysis of the specific reasons, 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
<tx:annotation-driven transaction-manager= "TransactionManager"/>
and use annotations on classes that handle business logic ?
Note: @Transactional can be annotated on a method or on a class, annotations to all methods on a class use things management
?
@Servicepublic class Customerserviceimpl implements CustomerService {@Transactional public void Savecustomer (Cust Omer customer) {Customerdaoimpl.savecustomer (customer); } ...}
@Service @transactionalpublic class Customerserviceimpl implements CustomerService {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:
Note: Spring management does not need to add this
<property name= "Current_session_context_class" >thread</property>
Hibernate4 No Session found for current thread reason