Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer data from one database to another with Hibernate?

Tags:

hibernate

I have an application A with a domain-model which is mapped to a database using Hibernate. I have another application B that uses exactly the same domain-model-classes as A and adds some additional classes.

My goal is to read data from database A in application B and transfer that data into the database of B (to make a copy of it). In addition, some the domain-classes of B have associations (OneToOne) to domain-classes of A (but in the database of B, of course).

What's the best strategy to accomplish this? I thought of two session factories and using Session.replicate() (how does that work?). Or should I better introduce an additional mapping layer between these two domain-models for loose coupling?

like image 231
cretzel Avatar asked Oct 02 '08 13:10

cretzel


People also ask

How does hibernate interact with database?

Hibernate is an Object Relational Mapping tool which allows us to use objects for interaction with relational databases. It has many features like code first modeling, lazy loading, change tracking, caching, auditing etc.

Why Hibernate is better than JDBC?

Both Hibernate & JDBC facilitate accessing relational tables with Java code. Hibernate is a more efficient & object-oriented approach for accessing a database. However, it is a bit slower performance-wise in comparison to JDBC. Depending on the requirement of your project, you can choose the most suitable option.

How are relationships implemented in hibernate?

In hibernate we can map a model object into a relation/table with the help of @Entity annotation. The member variables of the model object corresponds to the table attributes. The table attributes in this case are Java primitive types, which maps into corresponding database specific primitive types.


3 Answers

I've done this before to transfer data between two different database types (in my case DB2 and MS SQL Server). What I did was to create two separate session factories, and give both of them the same list of mapping files. Then I simply read records from one, and saved them to the other.

Of course, this assumed that both data sources were identical.

like image 177
Ian McLaird Avatar answered Sep 20 '22 19:09

Ian McLaird


What is the purpose of the copying? Is that part of your application flow or logic? or just straight data copying?

If it is just for the sake of copying data over, there is no need to use hibernate. There are plenty of tools for it.

like image 20
DJ. Avatar answered Sep 19 '22 19:09

DJ.


Like others have pointed out, I think we need to know exactly what it is you're trying to accomplish. If you're doing a one time migration, there are better tools out there than Hibernate to do ETL (Extract, Transform, Load).

If you really insist on doing this in Hibernate (this applies to you also, Daniel), I'd do something like:

  1. Open session to database A.
  2. Read all entities of the type you're trying to copy (make sure lazy loading is disabled)
  3. Open session to database B.
  4. Save or update the entities.

I'd do this in a separate tool, rather than in application A or B.

On the other hand, if this is part of the functionality of your applications (e.g., application A is the admin console to the data, while application B consumes the data), you may want to do things a little differently. It's hard to say without knowing what exactly you're looking for.

Finally, something to look into (I don't think this is what you're looking for, but maybe it'll help you look at your problem in a different way) is Hibernate Shards (http://shards.hibernate.org/).

like image 23
Jack Leow Avatar answered Sep 18 '22 19:09

Jack Leow