views:

3016

answers:

8
A: 

Try using Bundle-ClassPath like this in your manifest

Bundle-ClassPath: ., /location/of/persistence.xml

bwobbones
No luck. I've tried: **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**
Grasper
Didn't show up right, so I put it in the post itself
Grasper
move persistence.xml to the root of the jar bundle? That should pick it up
bwobbones
A: 

The Meta-inf directory is not on the classpath. This should work by simply placing it under your src dirctory. If you want it in a separate location, then you will have to specify the Bundle-Classpath to include that directory. By default the classpath is '.'.

Robin
I just tried moving the META-INF to the src directory.. Still doesn't work. I've also tried explicitly pointing to the persistence.xml in the Bundle-Classpath and it doesn't work.
Grasper
@Grasper - Don't move the directory, move the persistence.xml file into source so when it is compiled it is on your classpath.
Robin
so I put persistence.xml into src, and I have '.' on the classpath and it still doesn't work. Thanks for your help :-)
Grasper
+1  A: 

I am not using persistence.xml but hibernate.cfg.xml which is similar:

src/main/resource/hibernate/hibernate.cfg.xml

In my Activator I am getting the file via the bundle context: Here is some example code how I do it and also reference that file:>

private void initHibernate(BundleContext context) {
     try {
      final AnnotationConfiguration cfg = new AnnotationConfiguration();
      cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
      sessionFactory = cfg.buildSessionFactory();

     } catch (Exception e) {
      // TODO Auto-generated catch block
     }
    }

As you can see line which gets the config file is:

context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml")

As you can see my hibernate.cfg.xml is NOT inside the META-INF folder. It is just in the root folder under /src/......

Hope that helps.

Christoph

Christoph
+3  A: 

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.

John Doe
EclipseLink is indeed the better choice for OSGi (by the way...its RI for JPA 2). According to the EL UserGuid you should load the EMFactory with this: HashMap<String, Object> properties = new HashMap<String, Object>(); properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader()); entityManagerFactory = new PersistenceProvider().createEntityManagerFactory(PU_NAME, properties);
lostiniceland
A: 

You need to have the directory that contains META-INF on the classpath. Each directory is searched for META-INF and if found, then persistence.xml is searched for.

If you put "META-INF" on the classpath, then you'd need another META-INF in that directory.

Doug Milo
A: 

hi ,

I have been trying to run a simple program to call the database using a bundle in eclipse.I have included the hibernate.cfg.xml and the mapping file too but it still its saying that : java.lang.NoClassDefFoundError: org/dom4j/DocumentException

even though i have included the dom4j.jar in the bundle build path

please suggest some work around and ways to go about it

Na_D
A: 

I'm getting the same problem.

I think eclipse link is the best option to use in a OSGi Environment. And there are no problem because you will work basically with the JPA implementation. When you need to move to Hibernate, just replace persintece.xml config and some libs.

Ed Pichler
Here is the tutorial:http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE
Ed Pichler
A: 

Hello,

you need to set property (for hibernate it will be different):

javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl

for call:

Persistence.createEntityManagerFactory(entityManagerFactoryName, properties)

to make it work.

And as mentioned before, you need classloader wrapping. You can use ClassloaderEntityManager from https://issues.apache.org/jira/browse/OPENJPA-1783 to do that.

Regards

Rafal Rusin