Home > Net >  How to spread the same persistence unit over multiple files?
How to spread the same persistence unit over multiple files?

Time:07-21

I have single persistence.xml file in resources\META-INF directory. Provider class, database connection settings, native queries are in this file. I'm reading BroadlearCommerce sources to study spring and hibernate better and noticed that they dont' have persistence.xml file at all. Instead, they have multiple files like persistence-common.xml, persistence-cms.xml etc.

Inside each of them there is persistence-unit name="blPU", that mean persistence unit with the same name exists in multiple files. First I thought that hibernate can detect them and join together info from the same PU because of "persistence-" prefix, but it turned out it can't, when I tried to do the same trick and placed my native queries into a separate file.

Probably it's a spring or some else library feature. Could you tell me how can I achieve this?

CodePudding user response:

Have a look at the class MergePersistenceUnitManager

There they merge all the persistence-*.xml files and then use DefaultPersistenceUnitManager from Spring to programmatically configure JPA.

  • Related