使用JPA检索单个值? [英] Retrieving single value using JPA?

查看:55
本文介绍了使用JPA检索单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA是否支持将单个值映射到标量数据类型?例如,要在以下查询中映射NUM

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

一些变量

int numerOfEmployees

还是在这种用例中必须使用JDBC?

or do I have to use JDBC for such use cases?

推荐答案

是的,您可以从JPQL查询中返回标量类型

Yes, you can return scalar types from JPQL queries

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

以及本机SQL查询中的

as well as from native SQL queries:

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

请注意,在某些情况下,结果类型可能取决于数据库,即它可能类似于BigDecimal.

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

这篇关于使用JPA检索单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆