Mapping one Entity class to two different databases (Oracle and Ingres)

心不动则不痛 提交于 2019-12-13 04:06:52

问题


I am newbie to ORM and JPA. I have a table called Table1 in Ingres. I need to copy Table1 from Ingres to Oracle. I have been successful in connecting to both databases. Is it possible to create only one Entity class called Table1 and then do this operation as follows: Get List from Ingres which has all the records from Table1. Persist List (wholly, if not then individually by collection element) to Oracle.

I would appreciate your suggestions and help.

Thanks, PK


回答1:


For this purpose, configure two persistence units pointing to different databases in persistence.xml file.

<persistence>
   <persistence-unit name="oracleDB">
      <jta-data-source>java:/OracleDB</jta-data-source>
       ...
   </persistence-unit>

   <persistence-unit name="ingresDB">
      <jta-data-source>java:/ingresDB</jta-data-source>
       ...
   </persistence-unit>
</persistence>

Persistence context is injected using annotation by the container for the given persistence-unit.

   @PersistenceContext(unitName="oracleDB")
   private EntityManager oracleEntityManager;

   @PersistenceContext(unitName="ingresDB")
   private EntityManager ingresEntityManager;

Then you can perform operation on databases by using respective entityManager instance.

Table name/structure must be same in both the databases & avoid using native functionality provided by vendors for portability.



来源:https://stackoverflow.com/questions/4595769/mapping-one-entity-class-to-two-different-databases-oracle-and-ingres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!