Category Archives: jpa

OSGI, JPA

Trying to use JPA in OSGI always gives me the error “javax.persistence.PersistenceException: No Persistence provider for EntityManager named …”. Reason: Persistence.createEntityManagerFactory() uses the ClassLoader to load resources. It seems to be the ThreadContext ClassLoader. So in OSGI it fails due to the OSGI classloader tricks. Using the tricks given in http://lsd.luminis.nl/jpa-persistence-in-osgi-with-openjpa/ I got this to work. That is, the following..

ClassLoader oldCL = Thread.currentThread().getContextClassLoader();

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

emf = Persistence.createEntityManagerFactory(“my-persistence-manager”);

Thread.currentThread().setContextClassLoader(oldCL);

And no matter what you do, you get the same error. Even if it never finds the persistence.xml file, or if you typoed tha name in it, or anything else. Oh what a wonderful error message, how informative.

You might think the EclipseLink being the reference JPA implementation you might get proper OSGI consideration but no. After all, it claims to be all OSGIfied. I guess that is no more than export some packages then. And after this fix, Hibernate works for me just as well as, just need to add some exports and manage the dependencies myself. Not a big deal compared to debugging this in the first place.

hiiiibernateee

another set of points

-ran into trouble trying to get hibernate to check my 2nd entity that is referencing the first one. the mysql create broke on the wrong innodb syntax noted before. and could not just leave the second one in as i wanted to reference the first one. solution. org.hibernate.dialect.MySQL5InnoDBDialect is a correct dialect to create innodb tables correctly. note the “5” in the middle. and search the hibernate reference manual for “mysql5”. nothing found, wheehee but it works.

-how do i reference an entity from another entity and have jpa persist it? value reference bm but not the other way around. solution. name the reference in value as @ManyToOne (several values can refer to the same bm). leave out @Column since it will be named same as the @Id in bm. uu yea seems to generate a sensible table at least..