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);
tx1.commit();
session1.close();
//By the reconcile with a second database
Session session2 = factory2.openSession();
Transaction txn2 = session2.beginTransaction();
session2.replicate(student, ReplicationMode.LATEST_VERSION);
txn2.commit();
session2.close();