1. Copy the jar packages required for our spring transaction control
2. Configure our Hibernatetemplate and transaction manager in the Spring container
<?xml version= "1.0" encoding= "UTF-8"?>
<!--spring configuration file: Import Constraints--
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--custom Java objects to be managed by spring
<bean id= "CustomerService" class= "Com.itheima.service.impl.CustomerServiceImpl" >
<property name= "Customerdao" ref= "Customerdao" ></property>
</bean>
<bean id= "Customerdao" class= "Com.itheima.dao.impl.CustomerDaoImpl" >
<property name= "Hibernatetemplate" ref= "Hibernatetemplate" ></property>
</bean>
<!--Java objects in a third-party jar package to spring for management--
<!--Create a Hibernatetemplate object--
<bean id= "Hibernatetemplate" class= "Org.springframework.orm.hibernate5.HibernateTemplate" >
<!--injected Sessionfactory--
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<!--Create Sessionfactory objects--
<!--when spring and hibernate are integrated, this Sessionfactory implementation class is provided by spring: Localsessionfactorybean,
Creating Sessionfactory is based on Hibernate's core configuration file
-
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property>
</bean>
<!--configuring Hibernate transaction Management--
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<!--configuring transaction management Notifications-
<tx:advice id= "Txadvice" >
<tx:attributes>
<tx:method name= "*" propagation= "REQUIRED" read-only= "false"/>
<tx:method name= "get*" propagation= "SUPPORTS" read-only= "true"/>
</tx:attributes>
</tx:advice>
<!--configuration Transactions AOP--
<aop:config>
<aop:pointcut expression= "Execution (* com.itheima.service.impl.*.* (..))" Id= "PT"/>
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "PT"/>
</aop:config>
</beans>
3. Transform our Layers
Public interface Customerdao {
void Save (customer customer);
List<customer> find ();
}
public class Customerdaoimpl implements Customerdao {
/**
* Crud operation of database by sessionfactory creating Session Object
* The process of creating Hiberantetemplete objects requires a dependency injection into a sessionfactory
*/
Private Hibernatetemplate hibernatetemplate;
public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {
This.hibernatetemplate = hibernatetemplate;
}
public void Save (customer customer) {
Hibernatetemplate.save (customer);
}
Public list<customer> Find () {
HQL Query All Customers
1. Get Query object, Session.createquery (HQL) 2. Pass query Parameters 3. Get query Results
Find method first argument, HQL statement, second argument, variable-length argument list
Return (list<customer>) Hibernatetemplate.find ("from Customer");
}
}
4. Test our hibernate and Spring integrations
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"Classpath:applicationContext.xml"})
public class Springandhibernatetest {
@Autowired
Private CustomerService CustomerService;
@Test
public void Getallcustomertest () {
List<customer> customers = Customerservice.getallcustomer ();
for (Customer customer:customers) {
SYSTEM.OUT.PRINTLN (customer);
}
}
@Test
public void Addcustomertest () {
Customer customer = new Customer ();
Customer.setcustname ("Zhao Zilong");
Customerservice.addcustomer (customer);
}
}
Here we test our Fgetallcustomertest method will be error, specific reasons, I also say not clear, but can look at this link 866669,
Org.springframework.orm.hibernate5.HibernateSystemException:CreateQuery is not valid without active transaction; Nested exception is org.hibernate.HibernateException:createQuery are not valid without active transaction
At Org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException ( sessionfactoryutils.java:219)
At Org.springframework.orm.hibernate5.HibernateTemplate.doExecute (hibernatetemplate.java:344)
At Org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession (hibernatetemplate.java:309)
At Org.springframework.orm.hibernate5.HibernateTemplate.find (hibernatetemplate.java:863)
At Com.itheima.dao.impl.CustomerDaoImpl.find (customerdaoimpl.java:25)
At Com.itheima.service.impl.CustomerServiceImpl.getAllCustomer (customerserviceimpl.java:18)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:606)
At Org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection (aoputils.java:302)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint (Reflectivemethodinvocation.java : 190)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:157)
At Org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation ( TRANSACTIONINTERCEPTOR.JAVA:99)
At Org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction ( transactionaspectsupport.java:281)
At Org.springframework.transaction.interceptor.TransactionInterceptor.invoke (transactioninterceptor.java:96)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (exposeinvocationinterceptor.java:92)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (jdkdynamicaopproxy.java:208)
At Com.sun.proxy. $Proxy 33.getAllCustomer (Unknown Source)
At Com.itheima.test.SpringAndHibernateTest.getAllCustomerTest (springandhibernatetest.java:23)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:606)
At Org.junit.runners.model.frameworkmethod$1.runreflectivecall (frameworkmethod.java:44)
At Org.junit.internal.runners.model.ReflectiveCallable.run (reflectivecallable.java:15)
At org.junit.runners.model.FrameworkMethod.invokeExplosively (frameworkmethod.java:41)
At Org.junit.internal.runners.statements.InvokeMethod.evaluate (invokemethod.java:20)
At Org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate ( RUNBEFORETESTMETHODCALLBACKS.JAVA:75)
At Org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate ( RUNAFTERTESTMETHODCALLBACKS.JAVA:86)
At Org.springframework.test.context.junit4.statements.SpringRepeat.evaluate (springrepeat.java:84)
At Org.junit.runners.ParentRunner.runLeaf (parentrunner.java:263)
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (springjunit4classrunner.java:254)
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild (springjunit4classrunner.java:89)
At Org.junit.runners.parentrunner$3.run (parentrunner.java:231)
At Org.junit.runners.parentrunner$1.schedule (parentrunner.java:60)
At Org.junit.runners.ParentRunner.runChildren (parentrunner.java:229)
At org.junit.runners.parentrunner.access$000 (parentrunner.java:50)
At Org.junit.runners.parentrunner$2.evaluate (parentrunner.java:222)
At Org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate ( RUNBEFORETESTCLASSCALLBACKS.JAVA:61)
At Org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate ( RUNAFTERTESTCLASSCALLBACKS.JAVA:70)
At Org.junit.runners.ParentRunner.run (parentrunner.java:292)
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (springjunit4classrunner.java:193)
At Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (junit4testreference.java:86)
At Org.eclipse.jdt.internal.junit.runner.TestExecution.run (testexecution.java:38)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:459)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:675)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:382)
At Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:192)
caused by:org.hibernate.HibernateException:createQuery is not valid without active transaction
At Org.hibernate.context.internal.threadlocalsessioncontext$transactionprotectionwrapper.invoke ( threadlocalsessioncontext.java:334)
At Com.sun.proxy. $Proxy 34.createQuery (Unknown Source)
At Org.springframework.orm.hibernate5.hibernatetemplate$29.doinhibernate (hibernatetemplate.java:866)
At Org.springframework.orm.hibernate5.hibernatetemplate$29.doinhibernate (hibernatetemplate.java:863)
At Org.springframework.orm.hibernate5.HibernateTemplate.doExecute (hibernatetemplate.java:341)
... More
The workaround is to comment in Hibernate's core configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.url" >jdbc:mysql:///ssh_280</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
<!--Configure C3P0 connection pool--
<property name= "Hibernate.connection.provider_class" >org.hibernate.connection.c3p0connectionprovider</ Property>
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<!--display SQL statements Show_sql true|false (default)--
<property name= "Hibernate.show_sql" >true</property>
<!--formatted SQL Format_sql true|false (default)--
<property name= "Hibernate.format_sql" >true</property>
<!--gets the session bound to the current thread
when spring and hibernate are integrated, the retrieved session object itself is the object bound to the current thread
so the Hibernate.current_session_context_class configuration can be omitted
* Session object is a session provided by spring, this session does not recognize the configuration of thread
* Need to configure Springsessioncontext
<property name= "Hibernate.current_session_context_class" >SpringSessionContext</property>
-
<!--the location of the mapping file
Class: Specifies the fully qualified class name of the entity class that has the JPA annotations configured
Resource:xml-Formatted mapping file
-
<mapping resource= "Com/itheima/entity/customer.hbm.xml"/>
</session-factory>
Take a look at the results we've tested.
Make a little progress every day!
SSH integrated three hibernate and spring integration