0

I'm trying to build a Spring-MVC portlet that interacts with two different tables of a MySQL database via JPA. I've read about 7 different posts which deal with this topic and with the error i get, but nothing helped me out. If i'm using only one of these tables the programm works perfect but if i add the other table as an entity i get an error. Both entity classes are auto generated by the JPA Tools of the Rational Application Developer by IBM.

My source:

Classes

Cause the *Manager.java are nearly the same i post the code only one time:

//imports
@SuppressWarnings("unchecked")
@JPAManager(targetEntity = entities.BookTbl.class)
public class BookTblManager {

private EntityManagerFactory emf;

public BookTblManager() {
    emf = EMFProvider.getEMF();
}

public BookTblManager(EntityManagerFactory emf) {
    this.emf = emf;
}

public void setEntityManagerFactory(EntityManagerFactory emf) {
    this.emf = emf;
}

private EntityManager getEntityManager() {
    if (emf == null) {
        throw new RuntimeException(
                "The EntityManagerFactory is null.  This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
    }
    return emf.createEntityManager();
}
//Create, delete, update methods

My EMFProvider.java (EntityManagerFactory Provider):

//Imports
public class EMFProvider {
private static EntityManagerFactory emf;
private static final String project = "SpringJPASample";

public EMFProvider() {
}

public static synchronized EntityManagerFactory getEMF() {
    if (emf == null) {
        emf = Persistence.createEntityManagerFactory(project);
    }
    return emf;
}
}

My persistence.xml:

...
<persistence-unit name="SpringJPA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>entities.User</class>
    <class>entities.BookTbl</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/portlet" />
        <property name="javax.persistence.jdbc.user" value="admin" />
        <property name="javax.persistence.jdbc.password" value="password" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>
...

Finally my error:

Caused by: java.lang.NoSuchMethodError: javax/persistence/Table.indexes()[Ljavax/persistence/Index;
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1108)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:774)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135)
at com.ibm.ws.jpa.management.JPAPUnitInfo.createEMFactory(JPAPUnitInfo.java:1584)
at com.ibm.ws.jpa.management.JPAPUnitInfo.createEntityManagerFactory(JPAPUnitInfo.java:1406)
at com.ibm.ws.jpa.management.JPAPxmlInfo.extractPersistenceUnits(JPAPxmlInfo.java:246)
at com.ibm.ws.jpa.management.JPAScopeInfo.processPersistenceUnit(JPAScopeInfo.java:119)
at com.ibm.ws.jpa.management.JPAApplInfo.processModulePUs(JPAApplInfo.java:167)
at com.ibm.ws.jpa.management.AbstractJPAComponent.startingModule(AbstractJPAComponent.java:451)
at com.ibm.ws.jpa.management.JPAComponentImpl.startingDeployedModule(JPAComponentImpl.java:729)
at com.ibm.ws.jpa.management.JPAComponentImpl.adjust(JPAComponentImpl.java:549)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.adjust(ApplicationMgrImpl.java:1069)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectAdjust(DeployedApplicationImpl.java:1394)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:627)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:968)
... 66 more

I'm using the Websphere Portal Server 8.5, Spring 4.3.2, Hibernate 5.1 and JPA 2.1.

I tried any solution which is mentioned in other posts i've found about this error and nothing worked. And i do not want to use two different databases not i want to use both tables in one class.

T. Jung
  • 3,327
  • 3
  • 11
  • 19
  • "NoSuchMethodError: javax/persistence/Table.indexes()" : you have messed up your JPA API jar and JPA provider jar versions. There is a JPA API v2.0 jar in the CLASSPATH somewhere. Yes, you say you have JPA 2.1 but there is also a JPA API v2.0 jar as well which is resolved first. 2 entities is irrelevant to the problem – Neil Stockton Oct 17 '16 at 15:04
  • Possible duplicate of [Error creating bean entityManagerFactory, NoSuchMethodError: javax/persistence/Table.indexes](http://stackoverflow.com/questions/39176612/error-creating-bean-entitymanagerfactory-nosuchmethoderror-javax-persistence-t) – Neil Stockton Oct 17 '16 at 15:04
  • Gonna check this. But why does the protlet work without any problems if i remove one of the entities? – T. Jung Oct 17 '16 at 15:14
  • "work" ? you mean you leave it down to chance which JPA API jar is loaded first. – Neil Stockton Oct 17 '16 at 15:17
  • Nope. I've checked my classpath 3 times there is no JPA API v.2.0 for sure. – T. Jung Oct 17 '16 at 15:26
  • Hibernate code (in the stack trace) is compiled against JPA API 2.1 where there is method _Table.indexes()_ and it isn't present at runtime, so it tries to call it and NoSuchMethodError. Fact. – Neil Stockton Oct 17 '16 at 15:43

2 Answers2

2

It could be due to the fact that WebSphere v8.5 does not support JPA 2.1. It only supports the JPA 2.0 API. So, the JPA 2.0 API gets loaded by WebSphere and this one is not sufficient (conflicts with) for use with this version of Hibernate, since it's based on JPA 2.1.

If you want to use JPA 2.1 with WebSphere v8.5, then you'll be limited to using application-managed persistence. More details on this configuration and usage can be found here: Websphere 8.5 with JPA 2.1

Community
  • 1
  • 1
Kevin S
  • 211
  • 2
  • 2
  • I removed JPA 2.1 cause as you said WebSphere v8.5 does not support JPA 2.1 and IBM already provides a JPA 2.0. But hibernate 5.1 uses the `javax.persistence.AttributeConverter` which is provided by _JPA v2.1_ so i used hibernate v4.2.21 instead and everything worked fine. – T. Jung Oct 24 '16 at 08:35
0

Try using the classloader viewer in WebSphere Application Server to resolve classloading issues https://www.ibm.com/support/knowledgecenter/SSAW57_8.0.0/com.ibm.websphere.nd.doc/info/ae/ae/utrb_classload_viewer_service.html