Merge is another name for an update of a row (and is much faster and
database-friendly than delete/insert which can require adjustments to the other
records whose FKs point to this one or can just fail because of that).
Refresh replaces the current values in the instance with the values from the
database. Merge happens in memory (in the persistence context) and might never
go to the database to flush the updates (if the transaction is rolled back or
the persistence context is cleared). It can (not required to it right away
though) let you know that the merge fails if the version value in your instance
is not the same as the one known to the persistence context. So it's not always
an override.
I'm not sure that I understand your point about setting an entity id to match
the persisted entity. All entity operations are on that entity, that is defined
by its id.
regards,
-marina
John O'Conner wrote:
> Forgive the simplistic questions of a JPA newbie:
>
> I have a simple Player entity and have been successful persisting player
> objects to Java DB. However, I'm confused about the merge operation, its
> purpose, and its usage.
>
> In trying to understand merge, I've decided that merge() is like
> refresh() in some ways (or maybe the opposite of refresh). It pushes an
> entity's properties back into the database, overwriting whatever is
> there. I noticed that merge() works on the intended database row only if
> I've set the entity id to match the persisted entity -- no surprise
> there. And all other fields in the row get overwritten with my new
> entity values. The term "merge" does confuse me a little -- no merging
> of anything seems to occur; the operation simply overwrites the db
> record, right?
>
> So two questions:
> Is this comparison to refresh a reasonable comparison?
> Is the merge operation effectively the same as removing an entity and
> persisting it again with the same id?
>
> Thanks,
> John O'Conner