运行简单的Hibernate应用程序“线程中的异常”主“java.lang.ExceptionInInitializerError”时出现此错误

时间:2015-07-22 05:05:32

标签: java mysql eclipse hibernate

我是hibernate的新手,在eclipse indigo和hibernate 4.3.10中尝试这个简单的代码。我正在使用jre7。

我在consol中得到了这个错误日志

 Jul 22, 2015 10:05:22 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Jul 22, 2015 10:05:22 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:326)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:291)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:295)
    at com.milind.hibernate.HibernateTest.createSessionFactory(HibernateTest.java:16)
    at com.milind.hibernate.HibernateTest.main(HibernateTest.java:29)
Caused by: java.lang.NullPointerException
      at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
       at org.hibernate.cfg.Environment.<clinit>(Environment.java:221)
    ... 5 more

我尽力解决此错误,请帮助我。 我是休眠的新手。

HibernateTest.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.milind.hibernate.dto.UserDetails;

public class HibernateTest {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

public static void main(String[] args) {
    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("Milind");

    SessionFactory sessionFactory = createSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();

}

}

UserDetails.java

import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class UserDetails {

@Id
private int userId;
private String userName;

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}

的hibernate.cfg.xml

 <?xml version='1.0' encoding='utf-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property  name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3535/students</property>

<property name="hibernate.connection.username">root</property>        
<property name="hibernate.connection.password">admin</property>  

<property name="hibernate.connection.autocommit">true</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>

<property name="show_sql">true</property>       

<!-- Mapping files -->
<mapping class="com.milind.hibernate.dto.UserDetails"/>

1 个答案:

答案 0 :(得分:0)

问题:hibernate配置文件 hibernate.cfg.xml 在类路径中不可用。

  

解决方案:将hibernate.cfg.xml文件添加到类路径。

如果您的hibernate配置文件未命名为&#34; hibernate.cfg.xml&#34;那么你还需要指定配置文件的名称。

Configuration cfg=new Configuration();
cfg.configure("filePath/your-config-file-name.cfg.xml");