eclipse java.lang.ExceptionInInitializer 在创建hibernate配置时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14054245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:42:02  来源:igfitidea点击:

java.lang.ExceptionInInitializerError in the creation of hibernate configuration

javaeclipsehibernateexceptionjboss

提问by a bouchenafa

I'm working on a Hibernate project and I configured everything.

我正在做一个 Hibernate 项目并且我配置了所有东西。

So, I generated the beans and hbm files.

所以,我生成了 beans 和 hbm 文件。

Then, I wrote a test class to test the project(I used the Clientclass)

然后,我写了一个测试类来测试项目(我使用了Client该类)

When I executed the code, the following exception was thrown:

当我执行代码时,抛出了以下异常:

java.lang.ExceptionInInitializerError
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.aness.test.HiberM.<clinit>(HiberM.java:12)
Exception in thread "main" 

the code is :

代码是:

import org.aness.beans.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HiberM {
 final static Logger logger = LoggerFactory.getLogger(Client.class);
public static void main(String[]arg){

    Configuration cfg = new Configuration().configure();
    SessionFactory sf =cfg.buildSessionFactory();
    Session s = sf.openSession();
    Transaction tx =s.beginTransaction();

    Client c =new Client();
    c.setRaisonSociale("peugeot algerie");
    c.setNumeroRc("3215468897");
    c.setIdentificationFiscale("888777999");
    c.setAdresseActivite("blida zone atlas");
    c.setTelephone("00213(0)25436996");
    c.setFax("00213(0)25436996");

    s.save(c);
    tx.commit();

   }

 }

that's the whole problem.

这就是整个问题。

the hibernate cfg file is : *

休眠 cfg 文件是:*

<?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 name="apurement">
 <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://localhost/apurement</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>

the client mapping is :

客户端映射是:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 27 d?c. 2012 11:47:54 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.aness.beans.Client" table="client" catalog="apurement">
    <id name="clientId" type="int">
        <column name="client_id" />
        <generator class="assigned" />
    </id>
    <property name="raisonSociale" type="string">
        <column name="raison_sociale" length="150" not-null="true" />
    </property>
    <property name="numeroRc" type="string">
        <column name="numero_rc" length="45" not-null="true" />
    </property>
    <property name="identificationFiscale" type="string">
        <column name="identification_fiscale" length="45" not-null="true" />
    </property>
    <property name="adresseActivite" type="string">
        <column name="adresse_activite" length="250" not-null="true" />
    </property>
    <property name="adressePersonelle" type="string">
        <column name="adresse_personelle" length="250" />
    </property>
    <property name="telephone" type="string">
        <column name="telephone" length="45" not-null="true" />
    </property>
    <property name="fax" type="string">
        <column name="fax" length="45" not-null="true" />
    </property>
    <set name="domiciliations" table="domiciliation" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="client_id" not-null="true" />
        </key>
        <one-to-many class="org.aness.beans.Domiciliation" />
    </set>
    </class>
</hibernate-mapping>

采纳答案by Luke Woodward

I think you've hit this issue.

我想你已经遇到了这个问题

It seems you're using SLF4J version 1.5.8 (or thereabouts), as the source code of org.slf4j.LoggerFactorywith tag 'SLF4J_1.5.8'has line numbers that match those in your stacktrace.

似乎您正在使用 SLF4J 版本 1.5.8(或相关版本),因为带有标记“SLF4J_1.5.8”源代码具有org.slf4j.LoggerFactory与堆栈跟踪中的行号匹配的行号。

I would recommend updating to later version of SLF4J.

我建议更新到更高版本的 SLF4J。

回答by Mukul Goel

Two possibilities :

两种可能性:

Your Hibernate.cfg.xmlis not on classpath. What folder is it in?

Hibernate.cfg.xml不在类路径上。它在什么文件夹中?

Else you may try updating the version of slf4jjar

否则你可以尝试更新slf4jjar的版本