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

解决方案 »

  1.   

    log4j.property文件写错了应该是
      

  2.   

    Caused by: java.lang.NullPointerException
    你的配置文件没配置全,Student没映射进去,所以你插入数据的时候没插进去,报错
      

  3.   

    at com.bjsxt.hibernate.Test.main(Test.java:14) 这不是说了空指针吗
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    估计是上面这行?你觉得这样能拿到hibernate的配置信息吗?
      

  4.   

    test java 一般都用 junit 你怎么直接用 main函数,ECLIPES加入junit包不用写main也可以直接执行
      

  5.   


    那该怎么拿hibernate的配置信息了、?
      

  6.   


    我写了Student.hbm.xml的
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class name="com.bjsxt.hibernate.Student">
    <id name="id" />
    <property name="name" />
    <property name="age" />
        </class>

    </hibernate-mapping>
      

  7.   

    写法是对的。。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是和日志相关的。
      

  8.   

    具体写法忘了你百度下 
    一般都用junit写测试方法
      

  9.   

    有可能这句有问题
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    配置文件没有配好,Student.hbm.xml放在什么地方了,也应该是src底下吧,和日志文件应该没有关系
      

  10.   

    看了半天,此问题肯定是你log4j配置有问题,把log4j 贴上来,看看就晓得了
      

  11.   

    我也碰到这样的问题(Hibernate 3.3的 Guide 手册上的例子,照着做的),能告诉解决的办法吗?
      

  12.   

    上述问题解决方法:
    <br/>(1)通过使用 MyEclipse 8自带的Hibernate3.2,没有任何问题;但是使用受动加载的Hibernate3.3的包,出现上述问题,故建议使用 eclipse自带的hibernate JAR,具体原因还请各位积极给出回答;
    <br/>(2)还有,就是hibernate.cfg.xml中的配置中应添加<property name="current_session_context_class">thread</property>属性。
      

  13.   

    纠正文字错误:
    上述问题解决方法:
    <br/>(1)通过使用 MyEclipse 8自带的Hibernate3.2,没有任何问题;但是使用受动手动加载的Hibernate3.3的包,出现上述问题,故建议使用 eclipse自带的hibernate JAR,具体原因还请各位积极给出回答;
    <br/>(2)还有,就是hibernate.cfg.xml中的配置中应添加<property name="current_session_context_class">thread</property>属性。