No CurrentSessionContext configured! 的解决方法

 

配置Hibernate时报下面的错误:

log4j:WARN No appenders could be found for logger (org.jboss.logging).

log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:986)
at test.Test.test(Test.java:15)
at test.Test.main(Test.java:28)

 

从网上找到的解决办法:

以下内容来至网上资料

 

1、用这种方法正确
static {

 

try {
configuration = new Configuration();

 

sessionFactory = configuration.configure().buildSessionFactory();
} catch (Throwable ex) {

 

throw new ExceptionInInitializerError(ex);
}
}

2、这种方式会出现异常

// static {
// try {
// // Create a configuration based on the properties file we've put
// Configuration config = new Configuration();
// config.addClass(Customer.class).addClass(Order.class);
// // Get the session factory we can use for persistence
// sessionFactory = config.buildSessionFactory();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }

 

第二种方式是针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式

 

或者在hibernate.cfg.xml中加入:

 

< property  name ="current_session_context_class" >thread </ property >
 
自己的解决方法是在hibernate.cfg.xml中添加<property name="current_session_context_class">thread</property> 这句。

 

转载于:https://www.cnblogs.com/szlwork/p/3332995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值