2

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("<");
      }
Tim Norman
  • 421
  • 1
  • 12
  • 31

4 Answers4

9

Try to use projection for getting result.

    session = sessionFactory.openSession();
    session.beginTransaction();
    Criteria criteria = session.createCriteria(Users.class);
    criteria.add(Restrictions.eq("Id", Id));
    criteria.setProjection(Projections.rowCount());
    long count = (Long) criteria.uniqueResult();
    session.getTransaction().commit();
   if(count != 0){
       System.out.println("present");
   else{
       System.out.println("absent");
   }
MayurB
  • 3,609
  • 2
  • 21
  • 36
  • Try to use this int count = ((Long) criteria.uniqueResult()).intValue(); – MayurB Jul 24 '13 at 05:00
  • What is the difference between our codes? I did not get the cause of issue, please have a look at this question as well http://stackoverflow.com/questions/17542157/how-to-avoid-nested-transactions-not-supported-error – Tim Norman Jul 24 '13 at 05:05
  • 1
    UniqueResult may return the size which can not be suffix by Interger.And Second code Session.Load wont throw exception until you access any method on Loaded object. – MayurB Jul 24 '13 at 05:31
  • I see all right, thanks please have a look at the other question as well. I am really interested in finding an answer for that. – Tim Norman Jul 24 '13 at 05:42
  • If i want to check multiple records then @MayurB – srinivas gowda Jan 20 '17 at 07:26
0

You can simply use Session.load method, if none exists a HibernateException will be thrown

Session session = // get session..
Users u = null;
try {
  u = (Users) session.load(Users.class, userId);
} catch (HibernateException e) {
  // No Users with id 'userId' exists
}
gerrytan
  • 40,313
  • 9
  • 84
  • 99
0

use session.get method,and use only exception for this case

 try
   {s = (student) session1.get(student.class, 16);
   System.out.println("the result is"+s.marks);
   } 


 catch(Exception e)
   {

    System.out.println("none");
 }
-1

If you have EntityManager instance Then you could do.

entityManager.find(Users.class, 1002L);

the second parameter is the primarykey Id of the record.

will return null if no records exist. will throw IllegalArgumentException if primaryKey is not inferred/unrecognizable.

abitcode
  • 1,420
  • 17
  • 24