title: Hibernate + JPA + Maven + Tomee + Eclipse
body: <p>I'm struggling to set up a very small Rest API using JAX-RS + Hibernate + H2 as DB on tomcat 8.5.</p>
  <p>After struggling so much with dependencies, testing Git sample , nothing worked so far.
  So :</p>
  <p>Pom is like :</p>
  <pre><code>&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.demo&lt;/groupId&gt;
      &lt;artifactId&gt;dataprovider&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;jersey.version&gt;2.36&lt;/jersey.version&gt;
          &lt;org.json.version&gt;20210307&lt;/org.json.version&gt;
          &lt;hibernate.version&gt;6.0.0.Final&lt;/hibernate.version&gt;
          &lt;h2.version&gt;[2.1.212,)&lt;/h2.version&gt;
          &lt;javax.persistence.version&gt;2.2&lt;/javax.persistence.version&gt;
          &lt;junit.jupiter.version&gt;5.8.1&lt;/junit.jupiter.version&gt;
          &lt;junit.platform.version&gt;1.8.1&lt;/junit.platform.version&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;release&gt;11&lt;/release&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2.3&lt;/version&gt;
              &lt;/plugin&gt;
  
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.11&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-common&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
              &lt;artifactId&gt;project&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-moxy&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-bean-validation&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.json&lt;/groupId&gt;
              &lt;artifactId&gt;json&lt;/artifactId&gt;
              &lt;version&gt;${org.json.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.orm&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;javax.persistence-api&lt;/artifactId&gt;
              &lt;version&gt;${javax.persistence.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
              &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
              &lt;version&gt;1.2.10&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
              &lt;version&gt;${h2.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
              &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
              &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
              &lt;artifactId&gt;junit-platform-suite&lt;/artifactId&gt;
              &lt;version&gt;${junit.platform.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  
  
  &lt;/project&gt;
  </code></pre>
  <p>Web.xml is like</p>
  <pre><code>&lt;display-name&gt;dataprovider&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  
  &lt;/welcome-file-list&gt;
  &lt;display-name&gt;DataProvider&lt;/display-name&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;DataProvider-servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
          &lt;param-value&gt;com.demo.provider.FutureApplication&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;DataProvider-servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/api/v1/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Persistence.xml :</p>
  <pre><code>&lt;persistence-unit name=&quot;dataproviderPU&quot;&gt;
          
   
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
   
          &lt;jta-data-source&gt;jdbc:h2:mem:db1&lt;/jta-data-source&gt;
          &lt;properties&gt;
              &lt;property name=&quot;hibernate.ejb.cfgfile&quot; value=&quot;hibernate.cfg.xml&quot;/&gt;
          &lt;/properties&gt;
   
        
      &lt;/persistence-unit&gt;
  </code></pre>
  <p>hibernate.cfg.xml</p>
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- JDBC Database connection settings --&gt;
          &lt;property name=&quot;connection.driver_class&quot;&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name=&quot;connection.url&quot;&gt;jdbc:h2:mem:db1&lt;/property&gt;
          &lt;property name=&quot;connection.username&quot;&gt;sa&lt;/property&gt;
          &lt;property name=&quot;connection.password&quot;&gt;&lt;/property&gt;
          &lt;!-- JDBC connection pool settings ... using built-in test pool --&gt;
          &lt;property name=&quot;connection.pool_size&quot;&gt;1&lt;/property&gt;
          &lt;!-- Select our SQL dialect --&gt;
          &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
          &lt;!-- Echo the SQL to stdout --&gt;
          &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          &lt;!-- Set the current session context --&gt;
          &lt;property name=&quot;current_session_context_class&quot;&gt;thread&lt;/property&gt;
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name=&quot;hbm2ddl.auto&quot;&gt;create-drop&lt;/property&gt;
          &lt;!-- dbcp connection pool configuration --&gt;
          &lt;property name=&quot;hibernate.dbcp.initialSize&quot;&gt;5&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxTotal&quot;&gt;20&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxIdle&quot;&gt;10&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.minIdle&quot;&gt;5&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxWaitMillis&quot;&gt;-1&lt;/property&gt;
          &lt;mapping class=&quot;com.demo.provider.entities.Lobby&quot; /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  <p>And finally , error is :</p>
  <pre><code> Jun 24, 2022 4:42:41 PM jdk.internal.reflect.NativeMethodAccessorImpl invoke
      SEVERE: ContainerBase.addChild: start: 
      org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dataprovider]]
  ...
      Caused by: org.apache.tomee.catalina.TomEERuntimeException: org.apache.openejb.OpenEJBException: Creating application failed: C:\Shared\Apps\tomeeWS\webapps\dataprovider: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f): java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f)
  
  
    ...
          Caused by: org.apache.openejb.OpenEJBException: Creating application failed: C:\Shared\Apps\tomeeWS\webapps\dataprovider: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f): java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f)
  ...
          Caused by: org.apache.openejb.OpenEJBException: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f): java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f)
  ...
  Caused by: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f)
   ...
         Caused by: java.lang.ClassCastException: class org.hibernate.jpa.HibernatePersistenceProvider cannot be cast to class javax.persistence.spi.PersistenceProvider (org.hibernate.jpa.HibernatePersistenceProvider is in unnamed module of loader org.apache.tomee.catalina.TomEEWebappClassLoader @149ab762; javax.persistence.spi.PersistenceProvider is in unnamed module of loader java.net.URLClassLoader @2f686d1f)
  </code></pre>
  <p>I suppose there is mismatch between version but I'm not able to find one fully fonctionnal...</p>
  <p>Does anyone have any ideas what I'm doing wrong ?</p>
  <p>Thanks</p>