I am learning JPA. I read about persistence.xml file. It can contain more that one <persistence-unit> tag under <persistence> tag.
Upto my undestanding, <persistence-unit> defines:
- db connection settings
- classes(entity classes), jar files, and mapping files
- provider information
Then why would we need to group the entities of our application into different <persistence-unit>. All the entities of any application should be in one <persistence-unit> tag.
The only reason that I think that we need more than one <persistence-unit> is when we need to establish connection with more than one datastore.
Q1. Are there any other situations when we need more than one <persistence-unit> tag?
A persistence unit represents a datastore. Your question has a bit the smell that you expect that you can have more than one datastore for exactly the same data. This makes no sense. The way the
<persistence-unit>works makes perfectly sense. Defining the DB connection settings, the entity mappings, etc for an unique and independent datastore. You can add more, but they would be fully independent from each other. If you’d like or expect to link multiple datastores with each other, then the solution should be sought at a lower level, namely in the datastore itself.