0

I know this question already has answers but I am unable to resolve my issue.

I have hibernate-commons-annotations-5.0.1.Final.jar, hibernate-core-4.3.5.Final.jar, hibernate-jpa-2.1-api-1.0.0.final.jar in the classpath.

Following is my HibernateUtil code:

public class HibernateUtil {

private static SessionFactory sessionAnnotationFactory;

private static SessionFactory buildSessionAnnotationFactory() {
    try {
        // Create the SessionFactory from hibernate-annotation.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure("hibernate-annotation.cfg.xml");
        System.out.println("Hibernate Annotation Configuration loaded");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        System.out.println("Hibernate Annotation serviceRegistry created");
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionAnnotationFactory() {
    if (sessionAnnotationFactory == null) sessionAnnotationFactory = buildSessionAnnotationFactory();
    return sessionAnnotationFactory;
}

}

Still I am getting the following error:

Initial SessionFactory creation failed.java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

Caused By: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at net.deldot.util.HibernateUtil.buildSessionAnnotationFactory(Unknown Source) at net.deldot.util.HibernateUtil.getSessionAnnotationFactory(Unknown Source)

I am using IntelliJ if that helps. Its not a maven project. I am not using jboss. There are no glassfish jars.I have read other answers that say this error occurs because of conflicting hibernate-jpa-2.1-api-1.0.0.final.jar and hibernate-jpa-2.0-api-1.0.0.final.jar. But I don't see hibernate-jpa-2.0 jar anywhere in my project. I don't know which jar is conflicting with hibernate-jpa-2.1 jar .How do I check that?

Alien
  • 15,141
  • 6
  • 37
  • 57
Ram
  • 79
  • 3
  • 16

2 Answers2

1

You are using hibernate-commons-annotations-5.0.1.Final.jar from Hibernate 5 with hibernate-core-4.3.5.Final.jar from Hibernate 4. It is incorrect.

@Table annotation with Table#indexes() method should be in the hibernate-jpa-2.1-api-1.0.0.Final.jar

https://stackoverflow.com/a/35569978/3405171

You can try to check where the old @Table annotation comes from using this approach

https://stackoverflow.com/a/37409315/3405171

And, please, use Maven or Gradle.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • replacing hibernate-commons-annotations-5.0.1.Final.jar with hibernate-commons-annotations-4.0.1.Final.jar didn't help. I added @Table(indexes = {}) in a class and it does point to hibernate-jpa-2.1-api-1.0.0.Final.jar. – Ram Oct 02 '18 at 20:43
  • It is an old project where I have been given the task of replacing old jdbc codes with Hibernate. It is not a Maven project but I guess I can convert it to Maven, but I am not sure what issues I'll be encountering then. – Ram Oct 02 '18 at 20:50
  • @Ram _I added @Table(indexes = {}) in a class and it does point to hibernate-jpa-2.1-api-1.0.0.Final.jar._ Yes. But It doesn't mean that `@Table` annotation comes from that library at runtime. You can use a class loader example from the second link to check that. – v.ladynev Oct 03 '18 at 12:16
  • Thankyou for poiting me to the link to find the jar that is being loaded at runtime. Turns out , its coming from /C:/oracle/Middleware/Oracle_Home/oracle_common/modules/javax.persistence_2.0.0.0_2-0.jar!/javax/persistence/Table.class. – Ram Oct 03 '18 at 15:18
  • So, I tried enabling jpa2.1 support for my weblogic installation as mentioned in https://docs.oracle.com/middleware/1213/wls/EJBAD/using_toplink.htm#EJBAD1510 . I made the change to commEnv.cmd as suggested, started weblogic server. The server console does display CLASSPATH=C:\oracle\Middleware\Oracle_Home\oracle_common\modules\javax.persistence_2.1.jar:C:\oracle\Middleware\Oracle_Home\wlserver\modules\com.oracle.weblogic.jpa21support_1.0.0.0_2-1.jar; while restarting, but again when I run my application, I see @Table is being loaded from the same jpa2.0 jar :( – Ram Oct 03 '18 at 15:19
  • @Ram You are welcome. Sorry can't help you with weblogic. Probably this is a topic for another question. – v.ladynev Oct 03 '18 at 15:36
0

Okay, not the best solution but a workaround. Since I was unable to enable weblogic's support for JPA2.1, I replaced Hibernate-core-4.3.5.Final.jar with Hibernate-core-4.2.21.Final.jar (JPA2.1 support was introduced in Hibernate 4.3.0). Not getting that error anymore.

Ram
  • 79
  • 3
  • 16