Search code examples
javadatabasejpajpql

Is there a "SELECT 1" in JPQL?


Is there some equivalent of "SELECT 1" or "SELECT 1 FROM DUAL" for JPQL? Like a simple test query.


Solution

  • Yes, there is getResultList, which returns query results as an untyped List. And you can use setMaxResults, limiting it to 1:

    query.setMaxResults(1).getResultList();
    

    Or else you can say the following which returns a single untyped result:

    query.getSingleResult()
    

    The EntityManager API also allows creation of native queries, which may be your simplest approach:

    entityManager.createNativeQuery("select 1 from sometable").getSingleResult();
    

    For details, see http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html.