Configure multiple data sources in JPA. You can configure multiple <persistence-unit> in persistence. XML according to JPA specifications,
Of course, persistence-unit names cannot be repeated:
<Persistence-unit name = "Tods" transaction-type = "JTA">
<Provider>
Org. Apache. openjpa. Persistence. persistenceproviderimpl
</Provider>
<JTA-data-source> JDBC/Tods </JTA-data-source>
<Class> com. IBM. Its. itmc. entity. Zone </class>
<Exclude-Unlisted-classes> true </exclude-Unlisted-classes>
<Properties>
<Property name = "openjpa. JDBC. schema" value = "Tods"/>
<Property name = "openjpa. JDBC. dbdictionary" value = "DB2"/>
<Property name = "openjpa. JDBC. synchronizemappings" value = "buildschema (foreignkeys = true)"/>
<Property name = "openjpa. log" value = "defaultlevel = warn, tool = Info"/>
</Properties>
</Persistence-unit>
<Persistence-unit name = "tads" transaction-type = "JTA">
<Provider>
Org. Apache. openjpa. Persistence. persistenceproviderimpl
</Provider>
<JTA-data-source> JDBC/testvd </JTA-data-source>
<Class> com. IBM. Its. itmc. entity. tadszone </class>
<Class> com. IBM. Its. itmc. entity. tadsroad </class>
<Exclude-Unlisted-classes> true </exclude-Unlisted-classes>
<Properties>
<Property name = "openjpa. JDBC. schema" value = "db2admin"/>
<Property name = "openjpa. JDBC. dbdictionary" value = "DB2"/>
<Property name = "openjpa. JDBC. synchronizemappings" value = "buildschema (foreignkeys = true)"/>
<Property name = "openjpa. log" value = "defaultlevel = warn, tool = Info"/>
</Properties>
</Persistence-unit>
Of course, you can also configure more data sources. Here I only have two.
When calling, you only need to specify different unit-names for the entitymanager of JPA:
@ Persistencecontext (unitname = "tads ")
Private entitymanager em;
Or
@ Persistencecontext (unitname = "Tods ")
Private entitymanager em;
Then, you can use em to access entities under different data sources. You do not need to modify Orm. xml. It must be unitname rather than name,
If it is only a data source, the datasource can be found by default by writing the name, but the @ persistencecontext (unitname = "tads") is not configured for multiple data sources ")
If you only write @ persistencecontext (name = "tads"), the Tods can not resolve. error will be reported.
This is just the experience of using JPA in was. I hope to bring you some tips.