• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA - difference between persist() and merge() methods

 
Witold Marshal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up to now I was sure that the main difference between persist(obj) and merge(obj) is that persisting is intended for creating new records in datebase from entity objects created by constructor call. I was also sure that merge, on the other side, is the method that merges detached entities (objects) to Persistence Context but there must exist appropriate records in the datebase for those entities. While ago, by mistake I have merged to Persistence Context an entity Object newly created with entity constructor that had no reflecting record in the datebase. After flushing and transaction ending the new record was added to the datebase as I would call persist() instead merge(). No exception was thrown. I'm really surprised with that. So, can anyone explain me what is the main difference between persist and merge?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is nice link that explain the difference between persist and merge with example.
 
Witold Marshal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot : )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic