Hibernate错误ExceptionInInitializerError

时间:2016-12-09 08:15:10

标签: hibernate eclipse-neon

我得到的错误

struct Student {
    let name: String
    let age: Int
}

var student = Student(name: "David", age: 17) 
           /* ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^-initializer call for the type 
                 \ type 'Student'                */
print(type(of: student)) // Student

print(student.name) // David
print(student.age)  // 17

我的代码在这里

    log4j:WARN No appenders could be found for logger (org.jboss.logging).
    log4j:WARN Please initialize the log4j system properly.
    Initial SessionFactory creation failed.java.lang.NoSuchFieldError: TRACE
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at hibernate.example.HibernateUtil.<clinit>(HibernateUtil.java:28)
    at hibernate.example.storeProduct.AddProduct(storeProduct.java:20)
    at hibernate.example.storeProduct.main(storeProduct.java:13)
    Caused by: java.lang.NoSuchFieldError: TRACE
    at org.jboss.logging.Log4jLogger.translate(Log4jLogger.java:64)
    at org.jboss.logging.Log4jLogger.doLog(Log4jLogger.java:44)
    at org.jboss.logging.Logger.trace(Logger.java:107)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.registerStrategyImplementor(StrategySelectorImpl.java:53)
        at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.addDialect(StrategySelectorBuilder.java:237)
        at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.addDialects(StrategySelectorBuilder.java:188)
    at org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder.buildSelector(StrategySelectorBuilder.java:154)
    at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:222)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
    at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:87)
    at hibernate.example.HibernateUtil.<clinit>(HibernateUtil.java:24)
    ... 2 more

Product.java类

package hibernate.example;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  


public class storeProduct {

    public static void main(String[] args) 
{  
    AddProduct("Pents", "GUCCI BRAND", 500, 30, "PATA NAI");
}
public static void AddProduct(String pName, String pDes, double price, int items, String Brand) //      Access by Admin
{
    SessionFactory factory=HibernateUtil.getSessionFactory();
    Session session=factory.openSession();  
    //creating transaction object  
    Transaction t = session.beginTransaction();  
    product p = new product();
    p.setpName(pName);
    p.setpDescription(pDes);
    p.setPrice(price);
    p.setItemsAvailable(items);
    p.setBrand(Brand);
    session.persist(p);
    t.commit();//transaction is committed  
    if ( session.isOpen() ) session.close(); 
    System.out.println("Product successfully Inserted to db");
}

HibernateUtil.java CLASS

package hibernate.example;

public class product {
private int pId;
private String pName;
private String pDescription;
private double price;
private int itemsAvailable;
private String Brand;

public product() {
    // TODO Auto-generated constructor stub
}
public String getBrand() {
    return Brand;
}
public void setBrand(String brand) {
    Brand = brand;
}
public int getItemsAvailable() {
    return itemsAvailable;
}
public void setItemsAvailable(int itemsAvailable) {
    this.itemsAvailable = itemsAvailable;
}
public int getpId() {
    return pId;
}
public void setpId(int pId) {
    this.pId = pId;
}
public String getpName() {
    return pName;
}
public void setpName(String pName) {
    this.pName = pName;
}
public String getpDescription() {
    return pDescription;
}
public void setpDescription(String pDescription) {
    this.pDescription = pDescription;
}
public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}

}

product.hbm.xml文件

package hibernate.example;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Ali Hassan
 */
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
    // Create the SessionFactory from standard (hibernate.cfg.xml) 
    // config file.
    sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
    }

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

Hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="hibernate.example.product" table="product">  


<id name="pId"
    column="pId" 
    unsaved-value="0"
    >  
     <generator class="native"></generator>  
    </id>
    <property name="pName"></property>  
    <property name="Brand"></property>  
    <property name="price"></property>  
    <property name="itemsAvailable"></property>  
    <property name="pDescription"></property>               


 </class>  


</hibernate-mapping>

我厌倦了这个错误。从2天起获得相同的错误。甚至没有一个解决方案可行。我正在使用ECLIPSE NEON。包括所有的罐子enter image description here。我在网络项目中使用了休眠模式。相同的文件在具有相同jar的其他项目中运行。

2 个答案:

答案 0 :(得分:0)

尝试使用log4J 1.2.12或更高版本。

您可以在类路径中检查类org.apache.log4j.Level的冲突版本并尝试解决它。 TRACE级别在版本1.2.12或更高版本的log4j jar中可用,但您使用的是1.2.11,因此存在问题。

答案 1 :(得分:0)

用以下代码替换HibernateUtil文件: -

#include <type_traits> template< typename U, typename ... Ts > struct belong_to { // before C++17 value will have to be defined recursively on the head of Ts static constexpr bool value = (std::is_same< U, Ts >::value || ... ); using type = typename std::enable_if< value, U > ::type; }; // usage example: template< typename T > using testable = typename belong_to< T, int, float, const char >::type; template< typename T > void test ( testable< T > i ) { // test process } int main() { test< int > ( 3 ); test< float > ( 3.0 ); test< const char > ('c'); // test< signed char >( 1 ); does not compile!!! } com.mkyong.persistence;

import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;

public class HibernateUtil {

package

}