title: ClassNotFound error on JPA and OSGI project
body: <p>I have a problem with JPA on OSGI environment.</p>
  
  <p>Some information on environment and framework:</p>
  
  <ul>
  <li>Runtime environment is karaf 4.1.6.</li>
  <li>org.hibernate.osgi Version 5.2.9.Final.</li>
  <li>hibernate-jpa-2.1-api </li>
  <li>org.apache.aries.jpa.container Version   2.6.1</li>
  </ul>
  
  <p>In my project I need two different persistence units on (almost) the same entity classes, cause I need to write in two diffent Oracle schema.</p>
  
  <p>So I have two datasource like this (with different parameters):</p>
  
  <pre><code>&lt;blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"&gt;      
  
  
      &lt;bean id="oraDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.OracleDriver" /&gt;
          &lt;property name="url" value="jdbc:oracle:thin:@******" /&gt;
          &lt;property name="username" value="***" /&gt;
          &lt;property name="password" value="***" /&gt;
          &lt;property name="validationQuery" value="SELECT 1 FROM DUAL" /&gt;
          &lt;property name="initialSize" value="5"/&gt; 
          &lt;property name="maxIdle" value="5"/&gt;
          &lt;property name="maxTotal" value="20"/&gt;
      &lt;/bean&gt;
  
  
  
      &lt;service interface="javax.sql.DataSource" ref="oraDataSource"&gt;
          &lt;service-properties&gt;
              &lt;entry key="osgi.jndi.service.name" value="jdbc/oraDataSource"/&gt;
          &lt;/service-properties&gt;
      &lt;/service&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is a semplified persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;persistence version="2.0"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
               xmlns="http://java.sun.com/xml/ns/persistence"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  
    &lt;persistence-unit name="PU"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;non-jta-data-source&gt;osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/oraDataSource)&lt;/non-jta-data-source&gt;
      &lt;!-- orm class --&gt;
      .....
      &lt;class&gt;my.path.jpa.model.Pratica&lt;/class&gt;
      &lt;class&gt;my.path.jpa.model.Tag&lt;/class&gt;
      ......
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/&gt;
        &lt;property name="hibernate.jdbc.use_streams_for_binary" value="false"/&gt;
        &lt;!--&lt;property name="hibernate.show_sql" value="true"/&gt;--&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  
  
    &lt;persistence-unit name="SYM-PU"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;non-jta-data-source&gt;osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/symOraDataSource)&lt;/non-jta-data-source&gt;
      &lt;!-- orm class --&gt;
      .....
      &lt;class&gt;my.path.jpa.model.Pratica&lt;/class&gt;
      &lt;class&gt;my.path.jpa.model.Tag&lt;/class&gt;
      ......
      &lt;class&gt;my.path.jpa.model.sym.PraticaSym&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/&gt;
        &lt;property name="hibernate.jdbc.use_streams_for_binary" value="false"/&gt;
        &lt;!--&lt;property name="hibernate.show_sql" value="true"/&gt;--&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  
  &lt;/persistence&gt;
  </code></pre>
  
  <p>And this is my felix plugin configuration:</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
      &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
      &lt;version&gt;${maven.bundle.plugin.version}&lt;/version&gt;
      &lt;extensions&gt;true&lt;/extensions&gt;
      &lt;configuration&gt;
        &lt;instructions&gt;
          &lt;Bundle-SymbolicName&gt;${project.artifactId}&lt;/Bundle-SymbolicName&gt;
          &lt;Meta-Persistence&gt;META-INF/persistence.xml&lt;/Meta-Persistence&gt;
          &lt;Export-Package&gt;
            my.path.jpa*
          &lt;/Export-Package&gt;
          &lt;Import-Package&gt;           
              org.hibernate.proxy, javassist.util.proxy, oracle.jdbc, *
          &lt;/Import-Package&gt;
          &lt;DynamicImport-Package&gt;oracle.jdbc.driver&lt;/DynamicImport-Package&gt;
        &lt;/instructions&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;
  </code></pre>
  
  <p>When I try to install the feature that contains this bundle I have the following error <strong>only the first time</strong> I try to install:</p>
  
  <pre><code>2019-10-07T18:50:19,315 | ERROR | CM Configuration Updater (ManagedService Update: pid=[org.apache.aries.jpa.SYM-PU]) | lix.cm.impl.ConfigurationManager 1284 | 9 - org.apache.felix.configadmin - 1.8.16 | [org.osgi.service.cm.ManagedService, id=2531, bundle=8564/mvn:org.apache.aries.jpa/org.apache.aries.jpa.container/2.6.1]: Unexpected problem updating configuration org.apache.aries.jpa.SYM-PU
  org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [my.path.jpa.model.Tag]
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:348) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.&lt;init&gt;(AnnotationMetadataSourceProcessorImpl.java:104) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.&lt;init&gt;(MetadataBuildingProcess.java:147) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885) [8587:org.hibernate.core:5.2.9.Final]
      at org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:96) [8589:org.hibernate.osgi:5.2.9.Final]
      at org.apache.aries.jpa.container.impl.ManagedEMF.createAndPublishEMF(ManagedEMF.java:129) [8564:org.apache.aries.jpa.container:2.6.1]
      at org.apache.aries.jpa.container.impl.ManagedEMF.updated(ManagedEMF.java:125) [8564:org.apache.aries.jpa.container:2.6.1]
      at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1479) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1435) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141) [9:org.apache.felix.configadmin:1.8.16]
      at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109) [9:org.apache.felix.configadmin:1.8.16]
      at java.lang.Thread.run(Thread.java:748) [?:?]
  Caused by: java.lang.ClassNotFoundException: Could not load requested class : my.path.jpa.model.Tag
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:336) ~[?:?]
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:?]
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:?]
      at java.lang.Class.forName0(Native Method) ~[?:?]
      at java.lang.Class.forName(Class.java:348) ~[?:?]
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:345) ~[?:?]
      ... 16 more
  </code></pre>
  
  <p>If i retry to install the feature everything goes ok!</p>
  
  <p>The problem is always on the second Persistence Unit.
  If I try to invert the first with the second PU, declaring SYM-PU first than PU in persistence.xml, the same problem occurs on PU.</p>
  
  <p>Thank you in advance for your answers.</p>