org.hibernate.HibernateException: No CurrentSessionContext configured!

Alvin_yau 2014-04-06 04:45:09
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-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.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.Alvin_yau"/>

<bean id="addtionInterceptor" class="com.Alvin_yau.Spring.AOP.AddtionInterceptor"/>

<aop:config>
<aop:aspect id="addtionAspect" ref="addtionInterceptor">
<aop:before method="Before" pointcut="execution(public * com.Alvin_yau.Spring.Service..*.save(..))"/>
</aop:aspect>
</aop:config>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<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>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.Alvin_yau.Spring.Model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContext
</prop>

</props>

</property>
</bean>

<bean name="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

</beans>


————————————————————————————————————————————————
————————————————————————————————————————————————

@Component(value="userDao")
public class UserDaoImpl implements UserDao {

private SessionFactory sessionFactory=null;

public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public void add(Student student) {
// TODO Auto-generated method stub
Session session=sessionFactory.getCurrentSession();
session.save(student);
}
}


@Component(value="logDao")
public class LogDaoImpl implements LogDao {
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public void record(Log log) {
// TODO Auto-generated method stub
Session session=sessionFactory.getCurrentSession();
session.save(log);
}
}

————————————————————————————————————————————————
————————————————————————————————————————————————

@Component(value="userService")
public class UserService {
private UserDao userDao;
private LogDao logDao;

@Transactional
public void saveStu(Student student){
userDao.add(student);
Log log=new Log();
log.setLog_name(student.getName()+" recorded.");
log.setLog_date(new Date());
logDao.record(log);
}

public UserDao getUserDao() {
return userDao;
}

@Resource(name="userDao")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public LogDao getLogDao() {
return logDao;
}

@Resource(name="logDao")
public void setLogDao(LogDao logDao) {
this.logDao = logDao;
}
}

————————————————————————————————————————————————
————————————————————————————————————————————————

@Test
public void test() {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
System.out.println("****************************************************");
UserService us=(UserService) ctx.getBean("userService");
Student student=new Student();
student.setName("tom");
student.setPosition("student");
us.saveStu(student);
ctx.destroy();
ctx.close();
}
}




报错:
Junit:
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)

Console:
ERROR: HHH000302: Unable to construct current session context [org.springframework.orm.hibernate4.SpringSessionContext]
java.lang.reflect.InvocationTargetException

Caused by: java.lang.NoClassDefFoundError: org/hibernate/service/jta/platform/spi/JtaPlatform
at org.springframework.orm.hibernate4.SpringSessionContext.<init>(SpringSessionContext.java:56)
... 61 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.jta.platform.spi.JtaPlatform
...全文
276 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alvin_yau 2014-04-07
  • 打赏
  • 举报
回复
引用 2 楼 wlwlwlwl015 的回复:
Caused by: java.lang.NoClassDefFoundError: org/hibernate/service/jta/platform/spi/JtaPlatform 应该是因为少jar包或者jar包冲突,你当前版本的hibernate4.x的core包应该是没org.hibernate.service.jta.platform.spi.JtaPlatform这个类,你可以找一下。你把jar包换成4.2.0.Final试试,4.2版的有这个类。
成功了,ありがとうございます
sampson_li 2014-04-06
  • 打赏
  • 举报
回复
这个问题我原先也遇见过,你只要再加一个OpenSessionInView 过滤器就可以了
-江沐风- 2014-04-06
  • 打赏
  • 举报
回复
重新下载一下hibernate的jar包吧,把原先的删除,把新的部署上;
小灯光环 2014-04-06
  • 打赏
  • 举报
回复
Caused by: java.lang.NoClassDefFoundError: org/hibernate/service/jta/platform/spi/JtaPlatform 应该是因为少jar包或者jar包冲突,你当前版本的hibernate4.x的core包应该是没org.hibernate.service.jta.platform.spi.JtaPlatform这个类,你可以找一下。你把jar包换成4.2.0.Final试试,4.2版的有这个类。
jxplus 2014-04-06
  • 打赏
  • 举报
回复
少jar包。 可能你hibernate4的版本不对,可以换成hibernate-release-4.2.0.Final,4.2hibernate-core 中有这个类。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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