求助,关于spring与hibernate整合问题,使用getcurrentsession报错
applicationContext.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.studysai" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/studysai/pojo/Person.hbm.xml</value>
<value>com/studysai/pojo/Log.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut expression="execution(* com.studysai.service..*.*(..))"
id="service" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
PersonDAOImpl:
package com.studysai.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.studysai.pojo.Person;
@Repository
public class PersonDAOImpl implements PersonDAO{
private SessionFactory sessionFactory;
@Resource
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Override
public void save(Person person)
{
Session session = sessionFactory.getCurrentSession();
session.save(person);
System.out.println("saved");
}
}
LoginDAOImpl:
package com.studysai.dao;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.studysai.pojo.Log;
@Repository
public class LogDAOImpl implements LogDAO{
private SessionFactory sessionFactory;
@Resource
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Override
public void save(Log log)
{
Session session = sessionFactory.getCurrentSession();
session.save(log);
}
}
junit:
package com.studysai.service;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.studysai.dao.LogDAO;
import com.studysai.dao.LogDAOImpl;
import com.studysai.dao.PersonDAO;
import com.studysai.dao.PersonDAOImpl;
import com.studysai.pojo.Log;
import com.studysai.pojo.Person;
public class SaveServiceImplTest {
@Before
public void setUp() throws Exception
{
}
@Test
public void testSave()
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDAO personDAO = (PersonDAO)context.getBean("personDAOImpl");
LogDAO logDAO = (LogDAO)context.getBean("logDAOImpl");
Person person = new Person();
person.setName("lsi");
person.setPwd("meiyou");
Log log = new Log();
log.setMsg("save person ok");
personDAO.save(person);
logDAO.save(log);
}
}
使用getcurrentsession就报
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
如果使用opensession就没有问题,可是整合事务要由spring来管理要使用getcurrentsession,请问哪里出现了问题呢?