Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

orm

jpa

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

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

Object foo = em.createQuery("SELECT COUNT(t) FROM com.company.Thing t WHERE prop = :param")        .setParameter("param", value).getSingleResult(); 
like image 810
Simon Gibbs Avatar asked Aug 26 '10 10:08

Simon Gibbs


People also ask

What does query getSingleResult return?

NoResultExceptionJPA exceptionThrown by the persistence provider when Query. getSingleResult() or TypedQuery. getSingleResult()is executed on a query and there is no result to return.

What does EntityManager find return?

The find() method used to retrieve an entity defined as below in the EntityManager interface. T find(Class<T> entityClass, Object primaryKey) – Returns entity for the given primary key. It returns null if entity is not found in the database.

How does EntityManager work in JPA?

In JPA, the EntityManager interface is used to allow applications to manage and search for entities in the relational database. The EntityManager is an API that manages the lifecycle of entity instances. An EntityManager object manages a set of entities that are defined by a persistence unit.

What is EntityManager in Spring Data JPA?

The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities. The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.


2 Answers

As per the JPA specification, COUNT returns a Long:

4.8.4 Aggregate Functions in the SELECT Clause The result of a query

may be the result of an aggregate function applied to a path expression.

The following aggregate functions can be used in the SELECT clause of a query: AVG, COUNT, MAX, MIN, SUM.

For all aggregate functions except COUNT, the path expression that is the argument to the aggregate function must terminate in a state-field. The path expression argument to COUNT may terminate in either a state-field or a association-field, or the argument to COUNT may be an identification variable.

Arguments to the functions SUM and AVG must be numeric. Arguments to the functions MAX and MIN must correspond to orderable state-field types (i.e., numeric types, string types, character types, or date types).

The Java type that is contained in the result of a query using an aggregate function is as follows:

  • COUNT returns Long.
  • MAX, MIN return the type of the state-field to which they are applied.
  • AVG returns Double.
  • SUM returns Long when applied to state-fields of integral types (other than BigInteger); Double when applied to state-fields of floating point types; BigInteger when applied to state-fields of type BigInteger; and BigDecimal when applied to state-fields of type BigDecimal.

If SUM, AVG, MAX, or MIN is used, and there are no values to which the aggregate function can be applied, the result of the aggregate function is NULL.

If COUNT is used, and there are no values to which COUNT can be applied, the result of the aggregate function is 0.

The argument to an aggregate function may be preceded by the keyword DISTINCT to specify that duplicate values are to be eliminated before the aggregate function is applied.

Null values are eliminated before the aggregate function is applied, regardless of whether the keyword DISTINCT is specified.

like image 152
Pascal Thivent Avatar answered Sep 24 '22 23:09

Pascal Thivent


NB : there's a difference between JQPL and Native query

Query query = em.createQuery("SELECT COUNT(p) FROM PersonEntity p " ); 

query.getSingleResult().getClass().getCanonicalName() --> java.lang.Long

Query query = em.createNativeQuery("SELECT COUNT(*) FROM PERSON " ); 

query.getSingleResult().getClass().getCanonicalName() --> java.math.BigInteger

like image 43
lgu Avatar answered Sep 22 '22 23:09

lgu