繁体   English   中英

使用休眠本机查询的BigInteger结果错误

[英]Wrong BigInteger result using hibernate native query

我正在使用Hibernate4 + PostgresSql。 我需要从数据库中获取ID列表,而不需要获取整个对象。 我使用本机查询(因为它是分层查询),它看起来像:

String s = hierachical_part_skipped +"select id " +
                "from search_hierarchy " +
                "order by id";

Query q = getEntityManager().createNativeQuery(s);
List<Long> resList = new ArrayList<>();
List<BigInteger> biglist = q.getResultList();
for (Object id : biglist) {
    System.out.print(id+",");
    resList.add(id.longValue());
}

好吧,我的bigList中有三分之一是错误的值! 我有10,20,30,40,50,60,70,80,90,100 ... 27350而不是1,2 ... 2735(每个值加一个零)。 不通过Hibernate手动执行的本机查询返回正确的值。

我尝试将结果检索为“对象列表”,但结果相同

我找到了足够的解决方法

s.append("select id \\:\\:numeric " + //that's a hack
      "from search_department " +
      "order by id");
Query q = getEntityManager().createNativeQuery(s);
List<Long> resList = new ArrayList<>();
List<BigDecimal> biglist = q.getResultList();
for (BigDecimal id : biglist) {
    System.out.print(id + ",");
    resList.add(id.longValue());
}

不过,我想知道为什么BigInteger无法正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM