Retrieving single value using JPA?

前端 未结 1 1360
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 15:57

Is there any support by JPA to map single values to scalar data types? For example, to map NUM in the following query

SELECT COUNT(*) AS NUM FROM EMPLOYEES
<         


        
相关标签:
1条回答
  • 2021-01-11 16:16

    Yes, you can return scalar types from JPQL queries

    long num = ((Number)em.createQuery("select count(e) from Employee e")
                        .getSingleResult()).longValue();
    

    as well as from native SQL queries:

    long num = ((Number)em.createNativeQuery("select count(*) from Employees")
                        .getSingleResult()).longValue();
    

    Note that in some cases result type may depend on the database, i.e. it can be something like BigDecimal.

    0 讨论(0)
提交回复
热议问题