title: How to use two different entities in the same persistence.xml with JPA and Spring?
body: <p>I'm trying to build a <code>Spring-MVC</code> portlet that interacts with two different tables of a <code>MySQL</code> 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 <code>entity</code> i get an error. Both entity classes are auto generated by the <code>JPA Tools</code> of the <code>Rational Application Developer</code> by IBM. </p>
  
  <p>My source:</p>
  
  <p><a href="https://i.stack.imgur.com/FEKlk.png" rel="nofollow"><img src="https://i.stack.imgur.com/FEKlk.png" alt="Classes"></a></p>
  
  <p>Cause the <code>*Manager.java</code> are nearly the same i post the code only one time:</p>
  
  <pre><code>//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
  </code></pre>
  
  <p>My <code>EMFProvider.java</code> (<em>EntityManagerFactory Provider</em>):</p>
  
  <pre><code>//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;
  }
  }
  </code></pre>
  
  <p>My <code>persistence.xml</code>:</p>
  
  <pre><code>...
  &lt;persistence-unit name="SpringJPA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;class&gt;entities.User&lt;/class&gt;
      &lt;class&gt;entities.BookTbl&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/portlet" /&gt;
          &lt;property name="javax.persistence.jdbc.user" value="admin" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="password" /&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  ...
  </code></pre>
  
  <p>Finally my error:</p>
  
  <pre><code>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
  </code></pre>
  
  <blockquote>
    <p>I'm using the Websphere Portal Server 8.5, Spring 4.3.2, Hibernate 5.1 and JPA 2.1.</p>
  </blockquote>
  
  <p>I tried any solution which is mentioned in other posts i've found about this error and nothing worked. And i do <strong>not</strong> want to use two different databases not i want to use both tables in one class.</p>