由于org.hibernate.internal.util.ConfigHelper.getResourceAsStream中的java.lang.NullPointerException,Hibernate 4.3.5 SessionFactory创建失败

时间:2014-05-18 19:29:39

标签: hibernate

我正在尝试创建一个基本的hibernate java应用程序来简单地显示现有的表数据。我在创建SessionFactory对象的第一步陷入困境。以下是错误。

问题类似于此处提到的问题。 https://forum.hibernate.org/viewtopic.php?t=966926

May 19, 2014 12:17:05 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
May 19, 2014 12:17:05 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Initial SessionFactory creation failed.java.lang.ExceptionInInitializerError
java.lang.NullPointerException
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.test.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at com.test.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.test.HelloHibernate.getCities(HelloHibernate.java:17)
    at com.test.HelloHibernate.main(HelloHibernate.java:11)
Caused by: java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:324)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:289)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:293)
    at com.test.HibernateUtil.buildSessionFactory(HibernateUtil.java:11)
    ... 3 more
Caused by: java.lang.NullPointerException
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)
    ... 7 more

My Classpath包含一个用户库,其中包含来自hibernate 4.3.5发行版的所有jar文件。

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.4.Final.jar
hibernate-core-4.3.5.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
    mysql-connector-java-5.1.25-bin  ( This is mysql jdbc file)

我的Exclipse项目结构如下

Hibernatetest  (Project folder)
   src
       com.test                -Package
           City.java            -POJO
           HelloHibernate.java  -DAO
           HibernateUtil.java   -Helper Class
           City.hbm.xml         -Mapping 
       hibernate.cfg.xml       -config

配置文件位于包

之外

下面是我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.0.2:3306/world</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_schema">world</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <mapping resource="com/test/City.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

下面是我的映射文件City.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.test">
    <class name="City" table="city">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
        <property name="name" type="string" column="Name" />          
        <property name="countryCode" type="string" column="CountryCode"/>       
        <property name="district" type="string" column="District"/>        
        <property name="population" type="int" column="Population"/>           
    </class>
</hibernate-mapping>

下面是使用reveng.xml方法使用Hibernate工具为eclipse生成的City.java POJO类。

package com.test;
// Generated May 18, 2014 5:26:21 PM by Hibernate Tools 4.0.0
/**
 * City generated by hbm2java
 */
public class City implements java.io.Serializable {

    private static final long serialVersionUID = 6056273769318803807L;
    private int id;
    private String name;
    private String countryCode;
    private String district;
    private int population;

    public City() {
    }

    public City(String name, String countryCode, String district, int population) {
        this.name = name;
        this.countryCode = countryCode;
        this.district = district;
        this.population = population;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountryCode() {
        return this.countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getDistrict() {
        return this.district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public int getPopulation() {
        return this.population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

}

下面是我使用Hibernate 4.3.5文档创建的HibernateUtil.java

package com.test;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
     private static  SessionFactory sessionFactory = buildSessionFactory();
        private static SessionFactory buildSessionFactory() {
            try {             
          return new Configuration().configure().buildSessionFactory(
                    new StandardServiceRegistryBuilder().build() );             
            }
            catch (Throwable ex) {            
                System.err.println("Initial SessionFactory creation failed." + ex);
                System.err.println(ex.getCause());
                throw new ExceptionInInitializerError(ex);
            }   
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }     
}   

以下是我的主要应用程序类

package com.test;
import java.util.List;
import com.test.City;
import org.hibernate.Session;
import com.test.HibernateUtil;
public class HelloHibernate {

    public static void main(String[] args) {

        HelloHibernate dao = new HelloHibernate();
        List<City> cities = dao.getCities();
        for( City city : cities){
            System.out.println(city.getName());
        }
    }   
          public List<City> getCities(){
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();
                @SuppressWarnings("unchecked")
                List<City> cities = session.createQuery("from city").list();
                session.getTransaction().commit();
                session.close();
                return cities;          
            }
    }

我只是想将这个java文件作为Java应用程序运行。

2 个答案:

答案 0 :(得分:1)

有时这个例外

Caused by: java.lang.NullPointerException

  at org.hibernate.internal.util.ConfigHelper.getResourceAsStream

  (ConfigHelper.java:170)

        at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)

通常是在类路径中添加user library引起的。

解决方案:

不使用User Library添加jar文件,而是使用classpath添加jar。

right click on project -> Build path -> configure build path->
add external jars->browse to the jars location and select all required jars.

答案 1 :(得分:1)

order by

enter image description here