Given a Hibernate domain object, is there some way I can determine if it is live (i.e. exists in the database) other than searching for it? Is there some method a la HIbernate.isObjectLive(domainObj) or domainObj.amIAlive() that might be more efficient?
I’m relatively new to Hibernate. If “no” is the answer, I’m ok with that. Thanks in advance.
Hibernate would only know whether an object is ‘live’ by that definition by checking the database. You should use
Session.get(object.getClass(), object.getId())to see if that’s the case. It’ll return null if the object no longer exists in the database.Be aware that the
Sessionhas a cache, so if the object is in that cache already, but in the meantime has been deleted from the database,Session.get(...)will return a reference. For this reason you may want to get a newSessionbefore doing the check.