Java 无法正确获取查询结果
我已经编写了以下Java 无法正确获取查询结果,java,hibernate,Java,Hibernate,我已经编写了以下查询 select count (distinct asset.assetId) from Asset asset left join asset.assetTitles title left join asset.distTypes distTypes where title.program.id in (:progIdParam) and distTypes in (:lkpDistTypeId) and asset.active
查询
select
count (distinct asset.assetId)
from
Asset asset
left join asset.assetTitles title
left join asset.distTypes distTypes
where
title.program.id in (:progIdParam)
and distTypes in (:lkpDistTypeId)
and asset.active = 1
and asset.isShow = 1
and asset.classification = 'Internal Use'
我称之为:
private Long assetTitleListForIp = 0L;
assetTitleListForIp = (Long)entityManager
.createQuery(query)
.setParameter("progIdParam",progId)
.setParameter("lkpDistTypeId",LookupValueEnum.DIST_TYPE_INTL_PRODUCTION.getLkpId())
.getSingleResult();
如果我在eclipse控制台中启动查询并在DB中运行它,它将count显示为1。但在应用程序中,对于assetTitleListForIp,它将值赋值为零。我没有犯我所犯的小错误。有人能帮忙吗?可能你的铸造有问题
getSingleResult()
不会直接强制转换为Long
。尝试得到如下结果:
Object countObj=entityManager.createQuery(
"select count (distinct asset.assetId) from Asset asset left join "+
"asset.assetTitles title left join asset.distTypes distTypes where "+
"title.program.id in (:progIdParam) and distTypes in (:lkpDistTypeId) "+
"and asset.active =1 and asset.isShow = 1 and asset.classification "+
"='Internal Use'")
.setParameter("progIdParam",progId)
.setParameter("lkpDistTypeId",
LookupValueEnum.DIST_TYPE_INTL_PRODUCTION.getLkpId())
.getSingleResult();
Long count=Long.valueOf(""+countObj);
assetTitleListForIp=count;
看起来像是
assetTitleListForIp
是字段。我怀疑你的问题与查询本身无关。可能的问题:
- 查询以all方式运行,您看到的只是
assetTitleListForIp
在执行查询的方法中被局部变量屏蔽assetTitleListForIp
- 您正在从保存该字段的对象的某个其他实例读取该字段
assettilelistforip