Hibernate: check if object exists -
suppose, objects of type a stored in db. here's way load specific 1 db using hibernate:
org.hibernate.session session = ...; long id = 1; obj = session.load(a.class, id);
if object id=1 doesn't exist objectnotfoundexception. there way check if such object exists without having catch exception? have smth like:
org.hibernate.session session = ...; long id = 1; boolean exists = session.exists(a.class, id); if(exists){ // smth..... }
couldn't find hibernate docs...
you can use session.get
:
public object get(class clazz, serializable id) throws hibernateexception
it return null if object not exist in database. can find more information in hibernate api documentation.
Comments
Post a Comment