2012-02-01 99 views
7

我有一個嚴重的問題與休眠。我跟着各種書籍UND在線教程,但我得到了同樣的異常「的ExceptionInInitializerError」明顯受到HibernateUtil.java線Hibernate buildSessionFactory()異常

SessionFactory sf = cfg.configure().buildSessionFactory(); 

我的Tomcat日誌拋出說以下內容:

Caused by: java.lang.ExceptionInInitializerError 
at de.marcelstuht.nerven2.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:23) 
at de.marcelstuht.nerven2.server.HibernateUtil.<clinit>(HibernateUtil.java:8) 
at de.marcelstuht.nerven2.shared.model.Account.<init>(Account.java:52) 
at de.marcelstuht.nerven2.server.GreetingServiceImpl.loginServer(GreetingServiceImpl.java:39) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569) 
... 21 more 

Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister 
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:180) 
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:131) 
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:345) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775) 
at de.marcelstuht.nerven2.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:18) 
... 29 more 

Caused by: org.hibernate.InstantiationException: could not instantiate test objectde.marcelstuht.nerven2.shared.model.Account 
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:49) 
at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68) 
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:75) 
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:143) 
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:498) 
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:142) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:158) 
... 34 more 

Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:46) 
... 44 more 

Caused by: java.lang.NullPointerException 
at de.marcelstuht.nerven2.shared.model.Account.<init>(Account.java:52) 
... 49 more 

Account.java

package de.marcelstuht.nerven2.shared.model; 

import java.io.Serializable; 
import java.util.List; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 

import org.apache.commons.codec.digest.DigestUtils; 
import org.hibernate.Session; 
import org.hibernate.criterion.*; 

import de.marcelstuht.nerven2.server.HibernateUtil; 

@Entity 
@Table(name = "account") 
public class Account implements Serializable { 

private static final long serialVersionUID = 2675108132819989138L; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "id") 
Long id; 

@NotNull 
    @Column(name = "username", unique = true) 
String username; 

@Column(name = "password") 
String password; 

@Column(name = "email") 
String email; 

protected Session session; 


public Account() { 
    session = HibernateUtil.getSessionFactory().openSession(); 
} 

public Account(Long id) { 
    this.id = id; 
    session = HibernateUtil.getSessionFactory().openSession(); 
} 

public Account(Long id, String username, String password, String email) { 
    this.id = id; 
    this.username = username; 
    this.password = password; 
    this.email = email; 
    session = HibernateUtil.getSessionFactory().openSession(); 

} 

public void setId(Long newid) { 
    id = newid; 
} 

public Long getId() { 
    return id; 
} 

public void setUsername(String user) { 
    username = user; 
} 

public String getUsername() { 
    return username; 
} 

public void setEmail(String mail) { 
    email = mail; 
} 

public String getEmail() { 
    return email; 
} 

public void setPassword(String newPass) { 
    password = newPass; 
} 

public void setNewPassword(String newPass) { 
    password = hashPassword(newPass); 
} 

public boolean checkPassword(String pass) { 
    pass = hashPassword(pass); 
    if (password.equals(pass)) return true; 
    else return false; 
} 

private String hashPassword(String password) { 
    return DigestUtils.shaHex("wudu390909dfh"+password); 
} 

public Account get(Long id) { 
    session.beginTransaction(); 
    Account result = (Account) session.get("account", id); 
    session.getTransaction().commit(); 
    return result; 
} 

public Long saveOrUpdate(Account account) { 
    if (account == null) { 
     return null; 
    } 

    session.beginTransaction(); 

    session.saveOrUpdate(account); 
    Long id = (Long) session.getIdentifier(account); 

    session.getTransaction().commit(); 
    return id; 
} 

public void delete(Account account) { 
    if (account == null) { 
     return; 
    } 
    session.beginTransaction(); 
    session.delete(account); 
    session.getTransaction().commit(); 
} 

public List<Account> getAccountByUsername(String user) { 
    session.beginTransaction(); 

    @SuppressWarnings("unchecked") 
    List<Account> result = session 
      .createCriteria(Account.class) 
      .add(Restrictions.eq("username", user)) 
      .list(); 
    session.getTransaction().commit(); 

    return result; 
} 

} 

HibernateUtil.java

package de.marcelstuht.nerven2.server; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.jboss.logging.Logger; 

public class HibernateUtil { 
private static final SessionFactory sessionFactory = buildSessionFactory(); 

protected HibernateUtil() { 

} 

private static SessionFactory buildSessionFactory() { 
    try { 
     // Create the SessionFactory from hibernate.cfg.xml 
     Configuration cfg = new Configuration(); 
     SessionFactory sf = cfg.configure().buildSessionFactory(); 
     return sf; 
    } catch (Exception ex) { 
     // Make sure you log the exception, as it might be swallowed 
     Logger.getLogger(HibernateUtil.class).error("Initial SessionFactory creation failed.", ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

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

的hibernate.cfg.xml

<!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> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://127.0.0.1:3306/ba</property> 
    <property name="connection.username">*myuser*</property> 
    <property name="connection.password">*mypassword*</property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 

    <!-- Disable the second-level cache --> 
    <!-- <property name="cache.provider_class">org.hibernate.cache.internal.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="hibernate.bytecode.provider">javassist</property> 

    <!-- Names the annotated entity class --> 
    <mapping class="de.marcelstuht.nerven2.shared.model.Account"/> 

</session-factory> 

我嘗試都休眠3.6.9以及4.0.1(在不同的Eclipse項目)。 我添加到項目中的JAR的屏幕截圖可以用Skitch:由https://skitch.com/marcelstuht/g7h8h/properties-for-nerven2

回答

12

aused:在de.marcelstuht.nerven2.shared.model.Account顯示java.lang.NullPointerException (Account.java:52) ... 49更多

從帳戶中的構造函數中刪除任何東西。你有一個循環依賴。和Hibernate使用公用無參數consructor其豆

  1. HibernateUtil的負載
  2. 這迫使靜態運行buildSessionFactory
  3. Hibernate提供無參數
  4. 這反過來調用HibernateUtil中實例化的帳戶....

相信我:除了POJO setter之外,沒有任何代碼公開的arg構造函數。沒有其他的。

+0

非常感謝。現在它可以工作。 – 2012-02-02 09:07:09

+0

我的問題不是一個加載的構造函數,但它實際上是一個循環依賴項。 我有一個SesssionFactory的靜態初始化器,但是,我的一個實體有一個需要設置Query的靜態值。 因此,Hibernate無法初始化,因爲該實體需要初始化它......(它遲了......希望有道理)。 – 2012-07-23 03:42:56

1

我之前遇到過這個問題。我最終發現,在註釋裝飾getter方法的實體中添加一個未註釋的方法可以讓你處於這種情況。 如果圍繞字段而不是getter方法進行註釋,則可以使用額外的未映射方法。

3

請檢查javassist.X.X.jar是否在您的類路徑中。

0

我也遇到了「無法獲得org.hibernate.persister.entity.SingleTableEntityPersister的構造函數」,並將javassist.X.X.jar添加到類路徑中。