I am currently testing out using OSGi. I am running this through Eclipse. I want to have my DAO layer as part of an OSGi solution, but my first stumbling block is this error:
Jun 29, 2009 6:12:37 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Ejb3Configuration configure
INFO: Could not find any META-INF/persistence.xml file in the classpath
I have tried putting the persistence.xml file in a lot of different places, to no avail. Any ideas on what I am doing wrong?
Is there a way to manually load the persistence.xml?
The activator looks like this:
package com.activator;
public class PersistenceActivator implements BundleActivator {
@Override
public void start(BundleContext arg0) throws Exception {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("postgres");
EntityManager em = emf.createEntityManager();
SimpleDaoImpl dao = new SimpleDaoImpl();
dao.setEntityManager(em);
}
@Override
public void stop(BundleContext arg0) throws Exception {
// TODO Auto-generated method stub
}
}
Here is what my directory structure looks like:
alt text http://www.freeimagehosting.net/uploads/7b7b7d2d30.jpg
Here is my Manifest.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Dao Plug-in
Bundle-SymbolicName: Dao
Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.4.0"
Bundle-Activator: com.activator.PersistenceActivator
Export-Package: com.dao.service
Require-Bundle: HibernateBundle;bundle-version="1.0.0"
HibernateBundle contains all of the Hibernate and Persistence Jars.
Here is my Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<!-- Sample persistence using PostgreSQL. See postgres.txt. -->
<persistence-unit name="postgres" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<!--
Comment out if schema exists & you don't want the tables dropped.
-->
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- drop/create tables @startup, drop tables @shutdown -->
<!-- Database Connection Settings -->
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="postgres" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test" />
<!-- Not sure about these... -->
<property name="hibernate.max_fetch_depth">16</property>
<property name="hibernate.jdbc.batch_size">1000</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.default_batch_fetch_size">500</property>
<!-- Hibernate Query Language (HQL) parser. -->
<property name="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<!-- Use c3p0 for the JDBC connection pool -->
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Things I have tried in the Manifest's Classpath with no luck:
Bundle-ClassPath: ., META-INF/persistence.xml
Bundle-ClassPath: ., ../META-INF/persistence.xml
Bundle-ClassPath: ., /META-INF/persistence.xml
Bundle-ClassPath: ., ./META-INF/persistence.xml
Bundle-ClassPath: ., META-INF
Bundle-ClassPath: ., ../META-INF
Bundle-ClassPath: ., /META-INF
Bundle-ClassPath: ., ./META-INF
Bundle-ClassPath: ., C:\Workspaces\OSGiJPA\Dao\META-INF\persistence.xml
Bundle-ClassPath: ., C:\Workspaces\OSGiJPA\Dao\META-INF
Use EclipseLink and forget about Hibernate and other implementations, because :
You'll have to play with the classloader too much... Thread.currentThread().setContextClassLoader(...)
You'll be tempted to set the bundle-classpath attribute and add dependencies manually instead of installing jar bundles.
You'll get provider not found errors or you might not be able to find persistence.xml
All the above efforts might not work after many attempts.
However, with EclipseLink it's a no brainer, the implementation was designed to work out of the box in an OSGI environment and there aren't any class loading headaches.