persist() and replicate() methods in Hibernate


  • It is equivalent to save(object) , but save(object) method returns identifier and persist(object) returns nothing.
  • Use save() method, If you are using generator class to generate the identifier and you want to know the identifier value.
  • Use persist() method, if you don’t want to know the generated identifier.

replicate(): Use to move the object from detached state to persistent state.

ReplicationMode has below attributes.

ReplicationMode.OVERWRITE: Overwrite existing rows when a row already exists.

ReplicationMode.IGNORE: Ignore replicated entities when a row already exists.

ReplicationMode.LATEST_VERSION: When a row already exists, choose the latest version.

ReplicationMode,EXCEPTION: Throw an exception when a row already exists.

Example on replicate()


See output, after calling replicate, emp object is associated with with the session.

Posted in hibernate and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.