java - Hibernate : Downside of merge() over update() -
I'm having a problem with a thrown by hibernate NonUniqueObjectException
.
Reading the docs and blog posts, I changed the call from update
to merge ()
, and resolved this problem.
I believe I understand the reason for the exception, and why the method has been set in terms of disconnected objects and session boundaries.
My question is this: Given that merge ()
always resolve the session object, or if it does not exist, then retrieve it, merge () Calling is usually a safer option than update ()
?
Merge ()
at update ()
? Calling merge () is generally a safe alternative to update () from
is?
As a way to avoid non-UNIQUE object exceptions, yes I think that it explains why JPA does not allow an update method.
What is the downside to using merge () on update ()?
An unknown user can think that he has a fresh managed unit. Some kind of
// myEntity (does not pass as a parameter does not manage) is a managed unit session returned by // merge operation. Merge (my entity); // "new value" has not been committed because myEntity myEntity.setMyProperty ("newValue") is not managed;
And if your organization is not involved in your persistence, then you probably do not want to pre-update default behavior but it should be avoided.
- Add a version (@Version) column 0 or NULL version indicates that an example is new and to be inserted, has not been updated
- Use the hibernate interceptor
- If you are sure you want You can use the following approach
...
Public Zero Update MyEntity (MyEntity updateableMyEntity); // Load Database MyEntity myEntity = (MyEntity) does not affect session.load (MyEntity.class, updateableMyEntity.getId ()); BeanUtils.copyProperties (My Entity, Updateable Minority); }
In this way you can update your unit without the merge or update method. Perhaps you want to see this question
relationship,
Comments
Post a Comment