T. Jung
T. Jung

Reputation: 3657

How to use two different entities in the same persistence.xml with JPA and Spring?

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.

Upvotes: 0

Views: 554

Answers (2)

Kevin S
Kevin S

Reputation: 211

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

Upvotes: 2

Alexandre Polozoff
Alexandre Polozoff

Reputation: 316

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

Upvotes: 0

Related Questions