I need to verify the existence of a record in database, but regardless of the id that I pass to the following code, it does not show anything. either the record exists or not.
session = HibernateUtil.getSession();
session.beginTransaction();
Query q = session.createQuery("select count(*) from Users where id = :id");
q.setInteger("id", id);
Integer count = (Integer)q.uniqueResult();
if(count > 0) {
System.err.println(">");
}
else {
System.err.println("<");
}
session.getTransaction().commit();
session.close();
I've used the following code as well but still have the same problem
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Users users = null;
users = (Users) session.load(Users.class, id);
}catch(Exception e){
System.err.println("<");
}