What does JPA EntityManager.getSingleResult() return for a COUNT query?

后端 未结 8 1007
北海茫月
北海茫月 2021-02-01 02:43

What does EntityManager.getSingleResult() return for a COUNT query?

So.. what is the precise runtime type of foo?

Object foo = em.createQuer         


        
8条回答
  •  生来不讨喜
    2021-02-01 03:20

    return query.getSingleResult() != null ? Integer.parseInt(query.getSingleResult().toString()) : 0;
    

    Second call query.getSingleResult() will throw org.hibernate.SessionException: Session is closed!

    To avoid the org.hibernate.SessionException: Session is closed! use as below

    Object obj = query.getSingleResult();
        if (obj != null) {
            Integer result = Integer.parseInt(obj.toString());
            return result;
        }
    
        return Integer.valueOf(0);
    

提交回复
热议问题