Shan Arshad
Shan Arshad

Reputation: 151

Using Hibernate in OSGi without defining packages/classes to scan property

I have the following situation :

Project A - Contains a DB-Configurations and Creates Session_Factory at Init.
Project B - Contains entities and using Project A to persist entities.
Project C - Contains entities and using Project A to persist entities.

I am working in OSGi based environment using karaf. I am making small components/Bundles which are using Project A to persist their entities. Project A will be loaded first always as others are dependent on it.

Project A will initiate Session Factory at start but without any packages/classes to scan for entities.

Now any package tries to persist the object using Project A will ended up in exception which is obvious

org.hibernate.MappingException: Unknown entity:

Is there any way i can persist the entities without scanning any packages/classes...?

Upvotes: 1

Views: 960

Answers (2)

GeertPt
GeertPt

Reputation: 17874

Hibernate doesn't play well with OSGi. See the caveats at http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch17.html#d5e5021

EclipseLink supports OSGi via the Gemini project, but that has similar limitations: http://wiki.eclipse.org/Gemini/JPA/Documentation/Limitations

Apache Aries JPA allows you to create a PersistenceUnit per Bundle via a Meta-Persistence header in the manifest: http://aries.apache.org/modules/jpaproject.html. This could allow you to share the persistence.xml in project A between bundles B and C, but that still creates two different PersistenceUnits/SessionFactories, I guess.

Upvotes: 1

white
white

Reputation: 1922

SessionFactory has to know about an entity before persisting. In your case, if you register some classes from B and C to A, SessionFactory would probably store some references to these classes, thus breaking modularity.

My suggestion for you would be:

  1. Project A: creates java.sql.DataSource and exports it as an osgi service.
  2. Projects B and C: import java.sql.DataSource and both create their own SessionFactory, DAO layer and Service layer, and export service layer as osgi services.

Upvotes: 1

Related Questions