Personal Operating Environment:
hibernate:4.3.5
spring-context:4.0.6
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>runtime</scope>
</dependency>
<!--Spring ORM--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!--Hibernate-core--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!--Hibernate 4 uses Jboss logging, but older versions slf4j for logging--
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--hibernate-c3p0--
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
</dependency>
</dependencies>
After integration, if Getcurrentsession is used in the place where the session was obtained, a mistake may be reported: "No Session found for the current thread", the cause of this error, 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
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>
Hibernate and Spring Integration