Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3618196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:43:15+00:00 2026-05-18T22:43:15+00:00

What is the difference between persist() and merge() in Hibernate? persist() can create a

  • 0

What is the difference between persist() and merge() in Hibernate?

persist() can create a UPDATE & INSERT query, eg:

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
A a=new A();
session.persist(a);
a.setName("Mario");
session.flush();

in this case query will be generated like this:

Hibernate: insert into A (NAME, ID) values (?, ?)
Hibernate: update A set NAME=? where ID=?

so persist() method can generate an Insert and an Update.

Now with merge():

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
Singer singer = new Singer();
singer.setName("Luciano Pavarotti");
session.merge(singer);
session.flush();

This is what I see in the database:

SINGER_ID   SINGER_NAME
1           Ricky Martin
2           Madonna
3           Elvis Presley
4           Luciano Pavarotti

Now update a record using merge()

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
Singer singer = new Singer();
singer.setId(2);
singer.setName("Luciano Pavarotti");
session.merge(singer);
session.flush();

This is what I see in the database:

SINGER_ID   SINGER_NAME
1           Ricky Martin
2           Luciano Pavarotti
3           Elvis Presley
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:43:15+00:00Added an answer on May 18, 2026 at 10:43 pm

    JPA specification contains a very precise description of semantics of these operations, better than in javadoc:

    The semantics of the persist
    operation, applied to an entity X are
    as follows:

    • If X is a new entity, it
      becomes managed. The entity X will be
      entered into the database at or before
      transaction commit or as a result of
      the flush operation.

    • If X is a
      preexisting managed entity, it is
      ignored by the persist operation.
      However, the persist operation is
      cascaded to entities referenced by X,
      if the relationships from X to these
      other entities are annotated with the
      cascade=PERSIST or cascade=ALL
      annotation element value or specified
      with the equivalent XML descriptor
      element.

    • If X is a removed entity,
      it becomes managed.

    • If X is a
      detached object, the
      EntityExistsException may be thrown
      when the persist operation is invoked,
      or the EntityExistsException or
      another PersistenceException may be
      thrown at flush or commit time.

    • For
      all entities Y referenced by a
      relationship from X, if the
      relationship to Y has been annotated
      with the cascade element value
      cascade=PERSIST or cascade=ALL, the
      persist operation is applied to Y.


    The semantics of the merge operation
    applied to an entity X are as follows:

    • If X is a detached entity, the state
      of X is copied onto a pre-existing
      managed entity instance X’ of the same
      identity or a new managed copy X’ of X
      is created.

    • If X is a new entity
      instance, a new managed entity
      instance X’ is created and the state
      of X is copied into the new managed
      entity instance X’.

    • If X is a
      removed entity instance, an
      IllegalArgumentException will be
      thrown by the merge operation (or the
      transaction commit will fail).

    • If X
      is a managed entity, it is ignored by
      the merge operation, however, the
      merge operation is cascaded to
      entities referenced by relationships
      from X if these relationships have
      been annotated with the cascade
      element value cascade=MERGE or
      cascade=ALL annotation.

    • For all
      entities Y referenced by relationships
      from X having the cascade element
      value cascade=MERGE or cascade=ALL, Y
      is merged recursively as Y’. For all
      such Y referenced by X, X’ is set to
      reference Y’. (Note that if X is
      managed then X is the same object as
      X’.)

    • If X is an entity merged to X’,
      with a reference to another entity Y,
      where cascade=MERGE or cascade=ALL is
      not specified, then navigation of the
      same association from X’ yields a
      reference to a managed object Y’ with
      the same persistent identity as Y.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Difference between a bus error and a segmentation fault? Can it happen that a
I've learned difference between sessionStorage (persist during session) and localStorage (persist forever if not
Can someone clarify the difference between a constructor function and a factory function in
Can someone point me the difference between POCO , Self Tracking Entities , POCO
The difference between Chr and Char when used in converting types is that one
What is the difference between early and late binding?
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Is there a performance difference between i++ and ++i if the resulting value is
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass
What is the difference between TrueType fonts and Type-1 fonts?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.