title: Equinox (OSGi) and JPA/Hibernate - Finding Entities
body: <p>I am trying to use Hibernate/Spring in an OSGi (Equinox) environment.  It works great if I explicitly point it to the Entity classes in the Persistence.xml:</p>
  
  <pre><code>    &lt;class&gt;com.es.t.eee.domain.StuffSource&lt;/class&gt;
      &lt;class&gt;com.es.t.eee.domain.PostalAddress&lt;/class&gt;
  </code></pre>
  
  <p>What I want is for Hibernate to "find" all of the Entity classes just as it does outside of the OSGi environment.</p>
  
  <p>Hibernate is looking in the correct bundle for the @Entities:</p>
  
  <pre><code>Searching mapped entities in jar/par: bundleresource://34/
  WARN  27-07 15:30:24,235 (InputStreamZippedJarVisitor.java:doProcessElements:41):
  Unable to find file (ignored): bundleresource://34/
  </code></pre>
  
  <p>It looks like it should work, but when it gets to the point of looking in the Bundle Jar for @Entities, an exception occurs, and I am not sure why.  I have included the important parts of the log that Hibernate is spitting out.  </p>
  
  <p><strong>Does anyone have any ideas what I am doing wrong or what the issue here is?</strong></p>
  
  <p>I am using:</p>
  
  <ul>
  <li>Hibernate Core 3.3.0.SP1 </li>
  <li>Hibernate Annotations 3.4.0.GA </li>
  <li>Hibernate Commons Annotations 3.1.0.GA</li>
  <li>Hibernate EntityManager 3.4.0.GA</li>
  <li>Equinox 3.4</li>
  <li>Spring Dynamic Modules 1.2.0</li>
  </ul>
  
  <p><strong>Here is where Hibernate parses the Persistence.xml</strong></p>
  
  <pre><code>INFO  27-07 15:30:24,110 (Version.java:&lt;clinit&gt;:15):
  Hibernate Annotations 3.4.0.GA
  INFO  27-07 15:30:24,110 (Environment.java:&lt;clinit&gt;:543):
  Hibernate 3.3.0.SP1
  INFO  27-07 15:30:24,110 (Environment.java:&lt;clinit&gt;:576):
  hibernate.properties not found
  INFO  27-07 15:30:24,126 (Environment.java:buildBytecodeProvider:709):
  Bytecode provider name : javassist
  INFO  27-07 15:30:24,126 (Environment.java:&lt;clinit&gt;:627):
  using JDK 1.4 java.sql.Timestamp handling
  INFO  27-07 15:30:24,157 (Version.java:&lt;clinit&gt;:14):
  Hibernate Commons Annotations 3.1.0.GA
  INFO  27-07 15:30:24,157 (Version.java:&lt;clinit&gt;:16):
  Hibernate EntityManager 3.4.0.GA
  DEBUG 27-07 15:30:24,219 (Ejb3Configuration.java:configure:312):
  Processing PersistenceUnitInfo [
      name: unit.postgresql
      persistence provider classname: null
      classloader: BundleDelegatingClassLoader for [DatabaseObjects (DatabaseObjects)]
      Temporary classloader: org.springframework.instrument.classloading.SimpleThrowawayClassLoader@11b50a1
      excludeUnlistedClasses: true
      JTA datasource: null
      Non JTA datasource: com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -&gt; 3, acquireRetryAttempts -&gt; 30, acquireRetryDelay -&gt; 1000, autoCommitOnClose -&gt; false, automaticTestTable -&gt; null, breakAfterAcquireFailure -&gt; false, checkoutTimeout -&gt; 1000, connectionCustomizerClassName -&gt; null, connectionTesterClassName -&gt; com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -&gt; 1012f6d821goxcok12zcw86|174f02c, debugUnreturnedConnectionStackTraces -&gt; false, description -&gt; null, driverClass -&gt; org.postgresql.Driver, factoryClassLocation -&gt; null, forceIgnoreUnresolvedTransactions -&gt; false, identityToken -&gt; 1012f6d821goxcok12zcw86|174f02c, idleConnectionTestPeriod -&gt; 0, initialPoolSize -&gt; 5, jdbcUrl -&gt; jdbc:postgresql://localhost:5432/test2, lastAcquisitionFailureDefaultUser -&gt; null, maxAdministrativeTaskTime -&gt; 0, maxConnectionAge -&gt; 0, maxIdleTime -&gt; 0, maxIdleTimeExcessConnections -&gt; 0, maxPoolSize -&gt; 100, maxStatements -&gt; 100, maxStatementsPerConnection -&gt; 0, minPoolSize -&gt; 3, numHelperThreads -&gt; 3, numThreadsAwaitingCheckoutDefaultUser -&gt; 0, preferredTestQuery -&gt; null, properties -&gt; {user=******, password=******}, propertyCycle -&gt; 0, testConnectionOnCheckin -&gt; false, testConnectionOnCheckout -&gt; false, unreturnedConnectionTimeout -&gt; 0, usesTraditionalReflectiveProxies -&gt; false ]
      Transaction type: RESOURCE_LOCAL
      PU root URL: bundleresource://34/
      Jar files URLs []
      Managed classes names []
      Mapping files names []
      Properties [
          hibernate.default_batch_fetch_size: 500
          hibernate.cache.provider_class: net.sf.ehcache.hibernate.EhCacheProvider
          hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
          hibernate.max_fetch_depth: 5
          hibernate.query.factory_class: org.hibernate.hql.ast.ASTQueryTranslatorFactory
          hibernate.format_sql: false
          hibernate.jdbc.batch_size: 1000
          hibernate.use_outer_join: true
          hibernate.archive.autodetection: class
          hibernate.show_sql: false
          hibernate.bytecode.provider: cglib]
  </code></pre>
  
  <p><strong>Here is where the error occurs, as it tries to find the entities:</strong></p>
  
  <pre><code>DEBUG 27-07 15:30:24,235 (Ejb3Configuration.java:getDetectedArtifacts:562):
  Detect class: true; detect hbm: false
  DEBUG 27-07 15:30:24,235 (Ejb3Configuration.java:getDetectedArtifacts:562):
  Detect class: true; detect hbm: false
  DEBUG 27-07 15:30:24,235 (AbstractJarVisitor.java:unqualify:116):
  Searching mapped entities in jar/par: bundleresource://34/
  WARN  27-07 15:30:24,235 (InputStreamZippedJarVisitor.java:doProcessElements:41):
  Unable to find file (ignored): bundleresource://34/
  java.lang.NullPointerException: in is null
      at java.util.zip.ZipInputStream.&lt;init&gt;(Unknown Source)
      at java.util.jar.JarInputStream.&lt;init&gt;(Unknown Source)
      at java.util.jar.JarInputStream.&lt;init&gt;(Unknown Source)
      at org.hibernate.ejb.packaging.InputStreamZippedJarVisitor.doProcessElements(InputStreamZippedJarVisitor.java:37)
      at org.hibernate.ejb.packaging.AbstractJarVisitor.getMatchingEntries(AbstractJarVisitor.java:139)
      at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:287)
      at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:614)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:360)
      at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:224)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:291)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
      at org.springframework.beans.factory.support.AbstractBeanFactory.isSingleton(AbstractBeanFactory.java:366)
      at org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean.afterPropertiesSet(OsgiServiceFactoryBean.java:235)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
      at java.security.AccessController.doPrivileged(Native Method)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
      at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:288)
      at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:145)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>