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?
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.
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.
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.
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.
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 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:
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/).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With