hibernate报错Exception in thread "main" java.lang.ExceptionInInitializerError
hibernate.cfg.xml(放在src下面)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
<mapping class="com.bjsxt.hibernate.Teacher"/>
</session-factory>
</hibernate-configuration>
Test.java
package com.bjsxt.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("zhangsan");
s.setAge(8);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}
}
报错信息
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.bjsxt.hibernate.Test.main(Test.java:14)
Caused by: java.lang.NullPointerException
at org.slf4j.LoggerFactory.singleImplementationSanityCheck(LoggerFactory.java:192)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:113)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
... 1 more
[解决办法]
log4j.property文件写错了应该是
[解决办法]
Caused by: java.lang.NullPointerException
你的配置文件没配置全,Student没映射进去,所以你插入数据的时候没插进去,报错
[解决办法]
at com.bjsxt.hibernate.Test.main(Test.java:14) 这不是说了空指针吗
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
估计是上面这行?你觉得这样能拿到hibernate的配置信息吗?
[解决办法]
test java 一般都用 junit 你怎么直接用 main函数,ECLIPES加入junit包不用写main也可以直接执行
[解决办法]
写法是对的。。
at org.slf4j.LoggerFactory.singleImplementationSanityCheck(LoggerFactory.java:192)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:113)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
slf4j是和日志相关的。
[解决办法]
[解决办法]
有可能这句有问题
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
配置文件没有配好,Student.hbm.xml放在什么地方了,也应该是src底下吧,和日志文件应该没有关系
[解决办法]
看了半天,此问题肯定是你log4j配置有问题,把log4j 贴上来,看看就晓得了
[解决办法]
测试的main函数出错了
[解决办法]
我也碰到这样的问题(Hibernate 3.3的 Guide 手册上的例子,照着做的),能告诉解决的办法吗?