日期:2014-05-20  浏览次数:20821 次

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没映射进去,所以你插入数据的时候没插进去,报错
------解决方案--------------------