Replicating object between two different datastores in Hibernate by R4R Team

The ReplicationMode determines how replicate() will deal with conflicts with existing rows in the database:

1. ReplicationMode.IGNORE: ignores the object when there is an existing database row with the same identifier

2. ReplicationMode.OVERWRITE: overwrites any existing database row with the same identifier

3. ReplicationMode.EXCEPTION: throws an exception if there is an existing database row with the same identifier

4. ReplicationMode.LATEST_VERSION: overwrites the row if its version number is earlier than the version number of the object, or ignore the object otherwise

We can Usecases for this feature include reconciling data entered into different database instances, upgrading system configuration information during product upgrades, rolling back changes made during non-ACID transactions and more.

It is sometimes useful to be able to take a graph of persistent instances and make them persistent in a different datastore, without regenerating identifier values.

//Here we retrieve a student from one database
Session session1 = factory1.openSession();
Transaction txn1 = session1.beginTransaction();
Student student = session1.get(Student.class, studentId);

//By the reconcile with a second database
Session session2 = factory2.openSession();
Transaction txn2 = session2.beginTransaction();
session2.replicate(student, ReplicationMode.LATEST_VERSION);
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!