views:

48

answers:

2

The error I'm receiving is listed here.

That's my HibernateUtil.java

package com.rosejapan;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;;

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;

    static
    {
        try
        {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new  AnnotationConfiguration().configure().buildSessionFactory();
        } catch(Throwable e)
        {
            System.err.println("Initial sessionFactory creation failed.  " + e);
            throw new ExceptionInInitializerError(e);
        }
    }

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

Everything looks all right... I've already included log4j-boot.jar in the CLASSPATH, but didn't resolved my problem.

+1  A: 

I solved that, using only annotations.

The following jars was used:

alt text

With the code:

Users.java

package firsthibernateapp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "users")
public class Users implements java.io.Serializable
{

    private static final long serialVersionUID = -7960806792183842504L;

    @Id
    private Integer id;
    @Column(name = "name")
    private String  myName;

    public Users()
    {

    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getMyName()
    {
        return myName;
    }

    public void setMyName(String myName)
    {
        this.myName = myName;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Users other = (Users) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(obj)))
        {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hash = 3;
        hash = 53 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

}

Main.java

package firsthibernateapp;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class Main
{
    public static void main(String[] args)
    {
        SessionFactory sessionFactory = new AnnotationConfiguration()
                .setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect")
                .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
                .setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/test_hibernate")
                .setProperty("hibernate.connection.username", "root")
                .setProperty("hibernate.connection.password", "root")
                .setProperty("hibernate.show_sql", "true")
                .setProperty("hibernate.format_sql", "true")
                .setProperty("hibernate.c3p0.acquire_increment", "1")
                .setProperty("hibernate.c3p0.idle_test_period", "100")
                .setProperty("hibernate.c3p0.max_size", "10")
                .setProperty("hibernate.c3p0.max_statements", "0")
                .setProperty("hibernate.c3p0.min_size", "5")
                .setProperty("hibernate.c3p0.timeout", "100")
                .addAnnotatedClass(Users.class)
                .buildSessionFactory();

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        // Get the persistent instance of the given entity class with the given identifier
        // and prints out its member (myName)

        Users user = (Users) session.get(Users.class, 1);
        System.out.println("The user is " + user.getMyName() + "\n");

        // commit the changes, and close

        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }
}

I hope this can be helpful to someone.

Wilhelm
+1  A: 

The java.lang.IllegalAccessError clearly indicates an incompatibility problem with the version of Log4J you are using (a constructor expected to be there is not) and this is not surprising because (and I don't mean to be rude here) your whole dependency management (illustrated by the screenshot) seems to be a BIG mess:

  • You have many unversioned libraries so it's impossible to know what you're using (in what version).
  • You seem to be mixing things from various origins (at least two).
  • You have several version of the same lib (e.g. cglib).
  • You are mixing different versions of slf4j artifacts which can be problematic and is strongly discouraged.

So, since you're obviously not using any dependency management solution, my advice would be to use the JARs provided with the Hibernate distribution (put them in a clean directory) or at least to align the slf4j artifacts (e.g. on version 1.5.10 i.e. the version used by hibernate annotations 3.4) and to use log4j-1.2.14.jar. Your current workaround is a temporary fix hiding the real problem.

Pascal Thivent