简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM