EclipseLink

Set JPA entities at runtime

We use JPA as persistence mechanism for EPoS. The JPA implementation for the next release will be EclipseLink and I was adding that over the weekend. One important thing: I don’t know the entity classes at compile time. That makes it impossible to add the class names to the persistence.xml directly. Somehow we need a way to specify the classes at runtime (well, at least at initialization time, when we boto up the EntityManagerFactory). That is something taht seems to be not part of the JPA spec for Java SE environments ( in Java EE, you can let it search the jar files for entity classes).

Ok, so we need a way to manipulate the content of the persistence.xml at runtime to add the class entries and I found way, hacking into the mechanism that reads the content of the persistence.xml.
Basically, what we do is, we occupy the stream that represents the file and manipulate it at runtime. To do so, we have to extend the initialization process, but, thanks to protected variables used by the EclipseLink team, this works without any manipulation of the EclipseLink source code. We have to extend some classes, to make it work, but thats it. And it turned out that the changes are rather minimal and the hack works straight forward. So, lets jump into the code and get things running. Still, keep in mind, we work on top of internal initialization code ! There is a good chance that this breaks with a new release, although I think it will be straight forward to keep things running…at least as long as the EclipseLink team sticks to protected access modifiers for some of their variables.

› Continue reading

Tags: , ,

Monday, February 9th, 2009 Other 3 Comments