配置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中加入: