简体   繁体   中英

How can I read data from two different Databases (DB2 and Oracle) and Persist it into another DB (SQL Server) using Hibernate?

I am migrating an application which is on old Unix server to Linux server and below are the scenarios I need to consider.

  1. Need to read data from DB2.
  2. Need to read data from Oracle.
  3. Need to save the above read data into Sql Server (this DB consumes data from above two
    databases on a daily/weekly basis).
  4. All these Databases are on different severs.

What is the best possible approach I can take? I am already connected to Db2 using Hibernate and able to read data. What do you think is better you use, Hibernate or JDBC and why? Please help me with your expertise.

Updated: Application will be retrieving data on a daily basis from both databases and should persist it in SQL Server. Do you think web services would be a best approach?

The purpose of an ORM is to abstract away the relational data slightly so you can work easier in your object oriented environment.
Your goal on the other hand is to migrate data from one relational database to an other. There is no reason to use an ORM. It would be even counter productive.
Now that doesn't mean that you should use JDBC (even though it is a better alternative). Why don't you just create a dump of the contents of your two databases and import them into the other?

You say Hibernate, I'm going to reach and say JPA on top of Hibernate. If I'm mistaken, then feel free to map the Hibernate concepts on top of the JPA concepts.

In the most basic form, you would need three EntityManagers from three, separate, EntityManagerFactories. Each EMF is tied to the respective Persistence Unit of each database.

Then you would need to load the entities from one EM, map the object to an entity in the destination EM, and then persist the entity.

EntityMangerFactory oracleEmf = getOracleEntityMangerFactory();
EntityMangerFactory db2Emf = getdb2EntityMangerFactory();
EntityMangerFactory sqlEmf = getSqlServerEntityMangerFactory();

EntityManager oracleEm = oracleEmf.createEntityManager();
EntityManager db2Em = db2Emf.createEntityManager();
EntityManager sqlEm = sqlEmf.createEntityManager();

OracleEntity oe = oracleEm.find(OracleEntity.class, oracleEntityPrimaryKey);
DB2Entity db2e = db2Em.find(DB2Entity.class, db2EntityPrimaryKey);
SQLServerEntity soe = new SQLServerEntity(oe);
SQLServerEntity sdb2e = new SQLServerEntity(db2e);
sqlEm.persist(soe);
sqlEm.persist(sdb2e);

That's the gist of it.

Now, if you in fact have entities that are common to both systems, you can probably share the same classes (ie have the same class in multiple persistence units). But I have not tried persisting an entity that is managed from one EntityManager in to another. However, an unmanaged entity should work just fine.

Whether an ORM is appropriate really depends on the complexity of the conversions from one DB to another, and complexity of the transactions.

If you're just dumping raw tables whole sale from one DB to another, then I'd just use JDBC and batch mass INSERT statements in to the destination DB.

But there may well be quite some complexity in the migration, making work with the ORM much easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM