code_junkie

Hibernate: check if object exists

Nov 11th, 2011
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. org.hibernate.Session session = ...;
  2. long id = 1;
  3. A obj = session.load(A.class, id);
  4.  
  5. org.hibernate.Session session = ...;
  6. long id = 1;
  7. boolean exists = session.exists(A.class, id);
  8. if(exists){
  9. // do smth.....
  10. }
  11.  
  12. public Object get(Class clazz,
  13. Serializable id)
  14. throws HibernateException
  15.  
  16. public Boolean exists (DTOAny instance) {
  17. Query query = getSession().
  18. createQuery("select 1 from DTOAny t where t.key = :key");
  19. query.setString("key", instance.getKey() );
  20. return (query.uniqueResult() != null);
  21. }
Add Comment
Please, Sign In to add comment