没有正确查询查询结果

时间:2013-11-14 05:54:44

标签: java hibernate

我写了以下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中运行它,则显示count为1.但在应用程序中,对于assetTitleListForIp,它将值赋值为零。我没有得到我所做的小错误。请有人帮忙吗?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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

  • 查询运行全部,您看到的是assetTitleListForIp
  • 的初始值
  • assetTitleListForIp被执行查询的方法中的局部变量屏蔽
  • 您正在从持有该字段的对象的其他实例中读取字段assetTitleListForIp