Reputation: 21971
I need to detect if an object is transient..if it is not I want to update via Hibernate.
Usually I do this by checking if the id has been set but I am worried this is not thread safe and I have another thread which is saving the object and its possible that the id is not set but the Hibernate is already in the process of saving the object.
How can I do this in a thread-safe way?
Thanks.
public void updateIfNotNew(PersistentObject a){
//if another object is in the process of saving....this update will not get persisted
if (a.id!=null){
session.update(a);
}
}
Upvotes: 3
Views: 5747
Reputation: 23352
I recommend you to look for the interface org.hibernate.Session
there you can find the method contains, this method will look if the given object it's associated with that session (persistent), if not, doesn't mean that it's detached, because it could be associated with other session if any.
Upvotes: 8