Hibernate buildSessionFactory() Exception
Asked Answered
A

4

8

I have a serious problem with hibernate. I followed various books und online tutorials, but I ever get the same Exception "ExceptionInInitializerError" obviously thrown by the HibernateUtil.java Line

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

My Tomcat log says the following:

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>

I tried both Hibernate 3.6.9 as well as 4.0.1 (in different Eclipse Projects). A screenshot of the JARs I added to the project is available on Skitch: https://skitch.com/marcelstuht/g7h8h/properties-for-nerven2

Alexandraalexandre answered 1/2, 2012 at 22:19 Comment(0)
M
12

aused by: java.lang.NullPointerException at de.marcelstuht.nerven2.shared.model.Account.(Account.java:52) ... 49 more

Remove anything from your constructor in Account. You have a circular dependency. And Hibernate uses the public no args consructor for its beans

  1. HibernateUtil loads
  2. That forces the static to run buildSessionFactory
  3. Hibernate instantiates Account via no args
  4. That in turn calls HibernateUtil.....

Trust me: public no arg constructor with NO code, except POJO setters. Nothing else.

Madian answered 2/2, 2012 at 7:49 Comment(1)
My issue was not a loaded constructor, but it was in fact a circular dependency. I had a static initializer for the SesssionFactory, however, one of my entities had a static value that required a Query to be set. Therefore, Hibernate couldn't initialize, because that entity required it to be initialized... (It's late.... Hope that makes sense).Underproof
P
3

Please check the javassist.X.X.jar is in your classpath.

Priesthood answered 29/1, 2014 at 6:4 Comment(0)
C
1

I ran into this problem once before. I eventually discovered that adding a non-annotated method to the entity where annotations are decorating the getter methods, gets you in this situation. If you make the annotations around the fields rather than the getter methods, then you can have extra unmapped methods.

Christianity answered 9/7, 2012 at 17:11 Comment(0)
L
0

I too experienced the "Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister" and adding javassist.X.X.jar to the classpath took care of it.

Latitudinarian answered 11/6, 2015 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.