Interview questions
Question 241 of 254
FreeWhat is the difference between persist() and merge() in JPA?
Hibernate & JPA Internals
`persist()` makes a transient entity managed — it's meant for genuinely new entities and throws an exception if called on an entity that already has an assigned identifier matching an existing row (in most implementations). `merge()` takes a detached entity (or one with a set ID) and returns a new, managed copy with its state copied over — it doesn't attach the object you passed in itself, but rather a managed proxy/copy of it, which is why the returned reference from merge() must be used going forward, not the original object.
Advanced7 min
