What I have:
I need to update my entity
. I use Hibernatee conversation, that means that we already have entity
in the session cache.
public void handle(Request request, Session session) {
MyEntity updatedEntity = request.getEntity();
session.merge(updatedEntity); //rewrite all lazy collections
}
So I send object to client, client full up the object and send it back for update.
What the problem:
Lazy collections aren't sent to the client. As a result, if lazy collection hasn't been empty, than it will be overriden in session.merge(updatedEntity)
string
It happens because client knows nothing about element in those collections
Question:
How two merge entity
in correct way? Means without rewriting lazy collections.
EDIT:(how I work with my collections)
public class MyEntity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "entity_id")
private List<AnotherEntity> anotherEntities;
public void setAnotherEntities(List<AnotherEntity> anotherEntities) {
// we must work with the same instance of list (again, because of orphanRemoval)
this.anotherEntities.clear();
this.anotherEntities.addAll(anotherEntities);
}
}
I think that CascadeType.ALL
is the problem
You may use instead
cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DELETE})
You may also add to this set any other cascade option you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.