I have code using JPA and everything works fine in my development environment and in unit tests. But deploying my modules into the OSGi target environment, I regularly run into the weirdest class loading issues. I really like OSGi, but if I can't fix this once and for all, I'm going to get stark raving mad. And as long as I don't understand what classes need to be seen by which other classes, I'm never going to get the OSGi stuff set up properly.
So, as far as I can see, I have the following items that may or may not be visible from some piece of running code, let's call them "subjects":
- the JPA annotated entity classes
- a
persistence.xml
- the persistence API in
javax.persistence
- the persistence provider classes
And I have the following situations in my code:
- create an
EntityManagerFactory
and anEntityManager
- instantiate new entity objects
- passing those objects to the
EntityManager
to put them into its persistence context - keep using them, occasionally asking the EntityManager to save changes
- instantiating, using, and discarding entity objects without ever saving them to the database or otherwise explicitly calling the EntityManager's methods
- instead of instantiating entity objects, ask the EM to load them from the database, this leads to instantiation happening somewhere I don't see it.
- using, altering, saving and discarding these instances
So, in which of the above situations do I need which subjects to be visible?
I guess it's probably obvious that
- the persistence provider and entity classes need to be aware of javax.persistence
- the code that creates the EntityManager needs to see javax.persistence (and I guess the persistence provider, although that's not directly visible in any of my own code)