我是在做spring3.2.1与hibernate4.2Final整合的时候出的这个bug,在网上找了很多资料仍然解决不了
我的这个项目只是想测试在Service层上加上一事务,当向数据库存入一个User的时候,在log里面加上记录
下面给出我的相关代码,实体类User、Logo以及相应的接口类UserDAO与LogDAO就不给出了,对于逻辑没有什么影响:
用于处理User的DAO层的类UserDAOImpl:
package com.edu.hpu.impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.edu.hpu.dao.UserDAO; import com.edu.hpu.model.User; @Component(value="u") public class UserDAOImpl implements UserDAO { private SessionFactory sessionFactory; public void save(User user) { Session s = sessionFactory.getCurrentSession(); s.save(user); } public SessionFactory getSessionFactory() { return sessionFactory; } @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
用于处理Log的DAO层的类LogDAOImpl:
package com.edu.hpu.impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.edu.hpu.dao.LogDAO; import com.edu.hpu.model.Log; @Component(value="logDAO") public class LogDAOImpl implements LogDAO { private SessionFactory sessionFactory; @Override public void save(Log log) { Session s = sessionFactory.getCurrentSession(); s.save(log); } public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
用于联合处理User与Log的Service层次的类UserService类:
package com.edu.hpu.service; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.edu.hpu.dao.LogDAO; import com.edu.hpu.dao.UserDAO; import com.edu.hpu.model.Log; import com.edu.hpu.model.User; @Component(value="userService") @Scope(value="singleton") public class UserService { private UserDAO userDAO; private LogDAO logDAO; @Transactional public void save(User user) { userDAO.save(user); Log log = new Log(); log.setMsg("a user been add into database"); logDAO.save(log); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public LogDAO getLogDAO() { return logDAO; } @Resource(name="logDAO") public void setLogDAO(LogDAO logDAO) { this.logDAO = logDAO; } }
beans.xml里面的配置信息:
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:annotation-config /> <context:component-scan base-package="com.edu.hpu" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <context:property-placeholder location="jdbc.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 声明可以使用Annotation,而不是使用LocalSessionFactoryBean --> <property name="dataSource" ref="dataSource" /> <!-- 指定数据库的连接信息,即指定数据连接池 --> <property name="annotatedClasses"> <!-- 指定数据库中的实体 --> <list> <value>com.edu.hpu.model.User</value> <value>com.edu.hpu.model.Log</value> </list> </property> <property name="hibernateProperties"> <!-- 指定方言等属性 --> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
用于测试的Junit类:
package com.edu.hpu.sevice; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.edu.hpu.model.User; import com.edu.hpu.service.UserService; public class TestUser { @Test public void testSave() { ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)acx.getBean("userService"); service.save(new User()); } }
出现的问题情况如下,bug信息:
org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980) at com.edu.hpu.impl.UserDAOImpl.save(UserDAOImpl.java:18) at com.edu.hpu.service.UserService.save(UserService.java:23) at com.edu.hpu.sevice.TestUser.testSave(TestUser.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:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
出现bug的地点,是在getCurrentSession()这个地方。。求指教。。。