2

I will come to the point. I am new to Hibernate with JPA. I don't know the difference between Hibernate update() API and JPA merge() API. Is there any difference between theses two API's?

Atropo
  • 12,231
  • 6
  • 49
  • 62
user1679378
  • 1,370
  • 3
  • 17
  • 23

3 Answers3

2

Check these two links, SaveOrUpdate versus Merge in Hibernate and Hibernate: Merge vs SaveOrUpdate.

Atropo
  • 12,231
  • 6
  • 49
  • 62
1
  • update

    • On transient state : exception
    • on detached state :
      • if an attached instance already exist : exception
      • otherwise update persistent state and attach the given instance
  • merge

    • On transient state : update persistent state, return an attached instance, let given instance unchanged (and detached)
    • on detached state :
      • if an attached instance already exist : copy values of the given detached instance to the attached one, let given instance unchanged (and detached)
      • otherwise update persistent state, return an attached instance, let given instance unchanged (and detached)

see also hibernate update vs JPA merge on detached instance

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68
0

update() method can be used for objects both persistent and detached states. However, practically I can't imagine a scenario for using update() in persistent sate, where in you load an object into a session modify it and and update() in the same session.

As far as, merge() is concerned it is used for detached objects only. There is slight difference between how update() and merge() methods work in detached state. This difference is explained in detail with examples at the following location:

http://javahotpot.blogspot.in/2014/03/hibernate-difference-between-update-and.html

Yogen
  • 1