I solved using a Composite Persistence Units
Merging different persistence unit
-
30-06-2023 - |
Question
I'm developing a maven modular project with spring and vaadin that has the following structure
|-- parent
`-- pom.xml
|-- model1
| `-- pom.xml
|-- model2
-- pom.xml
|-- services
-- pom.xml
|-- web-app
-- pom.xml
model1 and model2 are two maven-archetype-quickstart that I modified in this way
|-- model1
|--src
|--main
|--java
|--main
|--resources
|-- META-INF
-- persistence.xml
|-- model2
|--src
|--main
|--java
|--main
|--resources
|-- META-INF
-- persistence.xml
Moreover in the model2 pom I have this dependency
<dependency>
<groupId>it.mycompany</groupId>
<artifactId>model1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
The persistence files are defined in the following way
Model1 persistence.xml
<persistence>
<persistence-unit name="model1Unit">
<class>it.mycompany.User</class>
<class>it.mycompany.UserDetail</class>
<class>it.mycompany.Address</class>
<class>it.mycompany.Agent</class>
<class>it.mycompany.Agency</class>
// and more others
</persistence-unit>
Model2 persistence.xml
persistence>
<persistence-unit name="model2Unit">
<class>it.mycompany.Agreement</class>
<class>it.mycompany.AgreementDetail</class>
// and more others
</persistence-unit>
Now my trouble is that within the mapping of the model2 project I want to do the following thing:
@Entity
public class Agreement{
@Id
@GeneratedValue
private long id;
private String description;
@ManyToOne
private Agent agent;
}
but the agent entity is contained in the model1 project/data source. Practically I want to use some entity of the model1 project in the model2 project to avoid of recreate them.
Is this possible? Any suggestion?
Solution 2
OTHER TIPS
You can try importing jar containing entities in Model2 persistence.xml without recreating them.
<jar-file>Model_1_Entity.jar</jar-file>