So it seems that there is no way to reattach a stale detached entity in JPA.
merge()
will push the stale state to the DB,
and overwrite any intervening updates.
refresh()
cannot be called on a detached entity.
lock()
cannot be called on a detached entity,
and even if it could, and it did reattach the entity,
calling 'lock' with argument 'LockMode.NONE'
implying that you are locking, but not locking,
is the most counterintuitive piece of API design I've ever seen.
So you are stuck.
There's an detach()
method, but no attach()
or reattach()
.
An obvious step in the object lifecycle is not available to you.
Judging by the number of similar questions about JPA,
it seems that even if JPA does claim to have a coherent model,
it most certainly does not match the mental model of most programmers,
who have been cursed to waste many hours trying understand
how to get JPA to do the simplest things, and end up with cache
management code all over their applications.
It seems the only way to do it is discard your stale detached entity
and do a find query with the same id, that will hit the L2 or the DB.
Mik
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…