persist() and replicate() methods in Hibernate

persist(): 

  • 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()

Output

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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">