求助,关于spring与hibernate整合问题,使用getcurrentsession报错

ldjyxm 2014-02-22 04:58:52
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,请问哪里出现了问题呢?

...全文
60 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ldjyxm 2014-02-22
  • 打赏
  • 举报
回复
自行解决,昏头了,测试的时候没走事务~

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧