首先把控制台输出给大家看看先:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cen.hibernate.model.StudentTest.main(StudentTest.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.   

    hibernate.cfg.xml配置文件如下:
    <?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/test1</property>
            <property name="connection.username">root</property>
            <property name="connection.password">123</property>
    <!-- JDBC connection pool (use the built-in) -->
            <!--<property name="connection.pool_size">1</property>
    --><!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.HSQLDialect</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">update</property>
            <mapping resource="com/cen/hibernate/model/Student.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
      

  2.   

    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 package="cem.cen.hibernate.model">
    <class name="Student" table="Student">
            <id name="id" column="id">
                <generator class="native"/>
            </id>
            <property name="age" />
            <property name="name"/>
    </class>
    </hibernate-mapping>
      

  3.   


    package com.cen.hibernate.model;public class Student {
    private int id;
    private int age;
    private String name;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
      

  4.   


    package com.cen.hibernate.model;
    import org.hibernate.cfg.Configuration;public class StudentTest {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student student = new Student();
    student.setId(1);
    student.setAge(23);
    student.setName("test");

    Configuration cfg = new Configuration().configure();
    }}
      

  5.   

    Configuration configuration = new Configuration();
    应该这样就可以了。
      

  6.   

    只要新建一个Configuration对象就出现空指针了。
      

  7.   

    <property name="dialect">org.hibernate.dialect.HSQLDialect</property> 这个不对