0

我写了以下内容query

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 中运行它,它将显示计数为 1。但在应用程序中,对于assetTitleListForIp,它将值分配为零。我没有得到我所犯的小错误。有人可以帮忙吗?

4

2 回答 2

0

看起来assetTitleListForIp是场。我怀疑您的问题与查询本身无关。可能的问题:

  • 查询全部运行,您看到的只是初始值assetTitleListForIp
  • assetTitleListForIp在执行查询的方法中被局部变量屏蔽
  • 您正在assetTitleListForIp从持有该字段的对象的其他实例中读取该字段。
于 2013-11-14T08:41:50.727 回答
0

可能你有铸造问题。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;
于 2013-11-14T06:06:43.940 回答