Java + Multiple persistence.xml files

This is probably more a Java issue than Vaadin - but I have not found an answer elsewhere - maybe someone here can help me??

I have created several different libraries (JPA) using Eclipse (and Vaadin JPA Containers in at least one of them). Each library has a persistence.xml file associated with it. When I include those libraries in a .war file, only one of those persistence.xml files gets used, and the other libraries become inaccessible. I tried including multiple persistence units in a single persistence.xml file, and that sort of worked - but only if that library gets loaded last, and I have not been able to force that - sometimes it seems to work, other times not.

Can anyone tell me how I should be packaging my libraries and/or persistence.xml files? This is becoming a major problem for my development and it seems like there should be a straightforward solution.

Much obliged,


Hi Neil,

each persistence.xml needs a root tag with attribute “name” which value must be unique. If your dependencies use persistence.xml files not beeing unique I think you can’t use them. But I am not the biggest expert :slight_smile:

I do have each one marked with the … tags. The problem is where do I put the actual text file in my project tree so it gets picked up and processed correctly so I can access the different databases…



I’m no expert, but I use multiple persistence units in a single persistence.xml file in WEB-INF/classes/META-INF. I’m using OpenJPA and EclipseLink and the folder structure was created when I chose the JPA facet for my project.


I just manually created a /src/META-INF/persistence.xml file in my Vaadin web project, and that did seem to work.
I have not added the JPA facet but I will try that in another project and see what happens. That seems to be the correct solution tho…

Procedural question for you then… If I have a dozen programs that use these databases, I need a persistence.xml file in each program. If I change one of the libraries, I then need to update the persistence.xml in every single program. Am I missing something or is that what I need to do? I suppose I could put the file someplace and link to it from all my programs, but otherwise, I don’t see an alternative to updating every executable…



Like I said, I’m no expert at JPA, but with the ease of use and lack of config I admire the technology. I need to do something similar to what you’re describing and my research leads me to believe the best solution is using EARs. From my understanding, you can share persistence entities across all applications on the same EE app container, although this is all new to me and I’ve got other priorities keeping me from going further.

You do have to write persistence.xml files for each app, but so long as your entity names don’t change you don’t have to redeploy any of your business logic when you make any schema changes, just the persistence entity EAR itself and any applications relying on the schema modifications. It is most likely from a memory standpoint to keep the persistence.xml file of each application specific to what that app needs rather than including every database/catalog/table for every app.

Even if this isn’t something you can do today, you could (as I’ve done) break out all your persistence info into a separate project and link it as a dependency of each app. Then the change to using EARs won’t be nearly as painful.

Something for me to look into - thanks…

It appears that my intermediate solution of linking to a centralized persistence.xml file will work. But I will try to take a look at the EAR solution.

Thanks for the help!


Make or use HibernateSessionFactoryUtils who configure your sessionFactory’s with get,open,current,close (for all DB what you want to add )

Configuration configuration = new Configuration(); // Base
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Configuration configuration2 = new Configuration();
serviceRegistry2 = new ServiceRegistryBuilder().applySettings(configuration2.getProperties()).buildServiceRegistry();
sessionFactory2 = configuration2.buildSessionFactory(serviceRegistry2);

[/code]Then when you need to use hc container or something else with hibernate use

SessionFactory sessionFactory = HibernateSessionFactoryUtil.get2SessionFactory(); and you got your second DB connection
Working with multiple DB you need session interceptor for destroying the both sessions or filtering it corectly.

Thanks for the info - won’t work for me right now since I forgot to mention that I’m using Eclipselink instead of Hibernate, but I’ll file it away for possible use in the future. Perhaps something similar could be done in the Eclipselink environment - if I have a chance, I’ll look into that too…


This is an old thread but wanted to share in case anyone else bumps into this issue. What worked for me was to create a META-INF\persistence.xml in the war (main) module that consolidates the information from the other modules. This overrides them in the package.