1

As per hibernate documentation:, there is a small example on how to use session.replicate() and usecases for the feature as:

Section 11.9. Replicating object between two different datastores:

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.

Can someone please help me in understanding how it is helpful in below cases:

  • upgrading system configuration information during product upgrades
  • rolling back changes made during non-ACID transactions and more.
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
learner
  • 6,062
  • 14
  • 79
  • 139

2 Answers2

3

This feature is not something you want to use generally. When you want to persist your objects with a previously generated identifier, you can use replicate().

Example Usage For System Up-gradation

Suppose you need to upgrade your system with some new features and removing some old ones. Now you want to migrate your existing database to the new one, facilitating your new requirements. The session.replicate() can be useful like below:

 myNewObject.setId(myOldObject.getId());
 myNewObject.setExistingProperty(myOldObject.getExistingProperty());
 myNewObject.setNewProperty("my new property");
 session.replicate(myNewObject, ReplicationMode.EXCEPTION);

Example Usage For Rolling Back Changes Made in non-ACID Transactions

Suppose your database became inconstant while doing some non-ACID transactions. Say, You have two classes in your system named, Course and Student. During a non-ACID transaction, say student1 object is persisted with course1, where student1 object is already given an id for course1, but course1 is not persisted due to some error. For that reason your DB becomes inconsistent (student1 is enrolled for a non-existent course). To repair your inconsistency, you simply create a new Course object with the data and id of course1 and persist it using session.replicate().

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

The replicate method didn't work for me because using a GeneratedValue on the id (which is very common) takes precedence over the id that you set. There's a bug for it here: https://hibernate.atlassian.net/browse/HHH-2716

The workaround is a custom generator and using the merge method instead. https://stackoverflow.com/a/48819098/728602

Cadell Christo
  • 3,105
  • 3
  • 21
  • 19