本文介绍了将一个Entity类映射到两个不同的数据库(Oracle和Ingres)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ORM和JPA的新手.我在Ingres中有一个名为Table1的表.我需要将表1从Ingres复制到Oracle.我已成功连接到两个数据库.是否可以仅创建一个称为Table1的Entity类,然后执行以下操作:从Ingres获取列表,该列表具有Table1中的所有记录.将列表持久化(如果不是,则按集合元素逐个列出)到Oracle.

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.

感谢您的建议和帮助.

谢谢,PK

推荐答案

为此,请在persistence.xml文件中配置两个指向不同数据库的持久性单元.

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;

然后,您可以使用相应的entityManager实例对数据库执行操作.

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.

这篇关于将一个Entity类映射到两个不同的数据库(Oracle和Ingres)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:54