1

SOLVED

I added the Persistence JPA2.1 to the library

The inicial error was solved, but now i have another one:

Initial SessionFactory creation failed: java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery

Any thoughts?

Inicial post

I'm having this error:

Initial SessionFactory creation failed: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
Exception in thread "main" java.lang.ExceptionInInitializerError
at modelo.util.HibernateUtil.<clinit>(HibernateUtil.java:29)
at modelo.dao.GenericDAO.getAll(GenericDAO.java:89)
at principal.Main.main(Main.java:34)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:100)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:252)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1386)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at modelo.util.HibernateUtil.<clinit>(HibernateUtil.java:25)
... 2 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more

Have no idea why I'm getting that exception

Main class:

public class Main {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {
       GenericDAO clienteDAO = new GenericDAO();
       List<Cliente> list = clienteDAO.getAll(Cliente.class);
       System.out.println(list.size());
       for(Cliente c : list)
         {System.out.println("-> " + c.getNombre());
       } 
     }
}

HibernateUtil class:

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;
}
}

Mapping Class.hbm.xml file:

 <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <!-- Generated 21/05/2017 17:46:05 by Hibernate Tools 4.3.1 -->
 <hibernate-mapping>
<class name="modelo.pojo.Cliente" table="cliente" catalog="basededatosprueba" optimistic-lock="version">
    <id name="codigo" type="java.lang.Integer">
        <column name="codigo" />
        <generator class="identity" />
    </id>
    <property name="nombre" type="string">
        <column name="nombre" />
    </property>
    <property name="direccion" type="string">
        <column name="direccion" />
    </property>
    <property name="telefono" type="string">
        <column name="telefono" />
    </property>
    <property name="cuit" type="string">
        <column name="cuit" />
    </property>
    <property name="cp" type="java.lang.Integer">
        <column name="cp" />
    </property>
    <property name="saldo" type="java.lang.Double">
        <column name="saldo" precision="22" scale="0" />
    </property>
    <property name="deuda" type="java.lang.Double">
        <column name="deuda" precision="22" scale="0" />
    </property>
</class>
</hibernate-mapping>

my hibernate.cfg.xml

 <hibernate-configuration>
 <session-factory>
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/basededatosprueba?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<mapping resource="modelo/pojo/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>

libraries (i also add hibernate-jpa-2.0-api-1.0.1.Final.jar downloaded from the comments)

libraries

I don't know why I'm getting that error. I already added the hibernate-jpamodelgen-1.2.0.Final.jar if you were asking. Hope someone can help me. Thanks

1
  • suppose this post might be helpful to you to understand the solution proposed. Commented May 22, 2017 at 0:15

2 Answers 2

1

Click below link to download hibernate-jpa-2.0-api-1.0.1.Final.jar and put it into the project library, hopefully your application will work fine.

http://repo1.maven.org/maven2/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar

Sign up to request clarification or add additional context in comments.

2 Comments

It changed the error Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
the above jar that I gave you has some dependent jar.. check them in maven and add them n your repo.. like I think the below jars need to be there hibernate-core,hibernate-validator,hibernate-commons-annotations,hibernate-jpa-2.0-api and hibernate-entitymanager
0

I faced the same issue. In my case hibernate-jpa-2.1-api-1.0.0.Final.jar solved the issue, along with hibernate-core.jar,hibernate-validator.jar,hibernate-commons-annotations.jar and hibernate-entitymanager.jar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.