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?
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.
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
来源:https://stackoverflow.com/questions/1211777/how-can-i-merge-extend-persistence-units-from-different-jars