Friday, April 1, 2011

To Merge or To Persist....

Merge the state of the given entity into the current persistence context.

more info here (from the JPA 1.0 specs)

Hibernate has this to say.

if you entityManager.persist(anEntity), and they you entityManager.persist(anEntity) again, you will get a UNIQUE_KEY contraint violation (it will save it twice with the same ID)

in doubt, just use merge and you will be safe:
if you invoke it with a unpersisted entity it will do an INSERT
if you invoke it with an already persisted entity it will do an UPDATE

No comments: