How can I merge / extend persistence units from different JARs?
Asked Answered
U

2

10

I use JPA persistence for my data models with Eclipselink as the persistence provider. I have a modular (OSGi) application and one of the modules contains the standard data model and a persistence unit that automatically includes all the entities from the package. The persistence provider is in another module, which works well.

Now I want a third module to add some entities to the persistence unit. How do I do that? I found this solution, which seems specific to Spring which I'm not using. The summary of that approach is to write a post processor that hooks into the persistence unit processing and manually merges <class> entries from the persistence.xml files.

Is it possible to merge persistence units? Can anybody suggest a workaround?

Uranalysis answered 31/7, 2009 at 10:16 Comment(2)
Are all three modules supposed to be in one .ear?Arbitrament
No, each module is a JAR that's then loaded into an OSGi container (Apache Felix, in my case). Eclipselink is either in its own JAR or in one of the modules and scans visible packages for persistence.xml files. This works well.Uranalysis
T
5

First, here is a link on Dynamic-JPA they offer a way to update entities dynamically. I would explore this option first.


Here is another link (PDF) from OSGi there are a few top level diagrams dealing with persistence and the EntityManagerFactory that may be useful.


an interesting approach but may be unrelated

Here is a link on creating persistence units programmaticly without using a persistence.xml file (which you can't really do, but the answers offer some insight to the problem). philk offers a suggestion on how to remove the provider from the persistence.xml file. If you follow this method, you may be able to combine your entities into a specific persistence unit.

"Well in the good old days of Hibernate I could just create a session and add my classes to it on the fly. Seems that never made it into SUNs JPA specs. However I have completely removed the provider from the persistence.xml and hand it to EL using the properties of the EMF. This seems to work ok. The only thing left in the persistence.xml is the spec of the classes. I guess it would not be too hard to expose the method that parses the classname in EL and creates the ClassDescriptor for it as a public method of the EL JPA Provider. "


Finally, we come to the hackish solution, doing has already been suggested, simply edit your persistence.xml files after the fact. I believe this would work (although, I haven't tried it) but it is definitely an option.

Titfer answered 6/8, 2009 at 21:16 Comment(0)
B
0

I have a setup where a WAR file contains the persistence.xml file. The solution already described here should be usable for JAR's, WAR's and EAR's. JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

Buttonball answered 26/3, 2014 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.