简体   繁体   English

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

[英]Wrong BigInteger result using hibernate native query

I'm using Hibernate4 + PostgresSql. 我正在使用Hibernate4 + PostgresSql。 I need to get a list of ids from my DB without getting a whole object. 我需要从数据库中获取ID列表,而不需要获取整个对象。 I use a native query (because it's hierarchical) and it looks like: 我使用本机查询(因为它是分层查询),它看起来像:

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());
}

Well, 1 time out of three my bigList is filled with wrong values! 好吧,我的bigList中有三分之一是错误的值! I have 10,20,30,40,50,60,70,80,90,100 ... 27350 instead of 1, 2... 2735 (a zero is added to each value). 我有10,20,30,40,50,60,70,80,90,100 ... 27350而不是1,2 ... 2735(每个值加一个零)。 Native query executed manually not through Hibernate returns correct values. 不通过Hibernate手动执行的本机查询返回正确的值。

I've tried retrieving result as List of Object, but the result was the same 我尝试将结果检索为“对象列表”,但结果相同

I have found a sufficient workaround 我找到了足够的解决方法

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());
}

Still, I'm wondering why BigInteger works incorrectly 不过,我想知道为什么BigInteger无法正常工作

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

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