【问题标题】:Wrong BigInteger result using hibernate native query使用休眠本机查询的错误 BigInteger 结果
【发布时间】:2016-06-14 01:49:06
【问题描述】:

我正在使用 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 中有 1 次填充了错误的值! 我有 10,20,30,40,50,60,70,80,90,100 ... 27350 代替 1, 2... 2735 (每个值都添加一个零)。 不通过 Hibernate 手动执行的本机查询返回正确的值。

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

【问题讨论】:

  • 删除一个将 id 值乘以十的触发器。
  • @Roman C,你在说什么触发器,为什么它有时只工作?

标签: java sql hibernate postgresql


【解决方案1】:

我找到了足够的解决方法

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 工作不正确

【讨论】:

  • 我对 postgres db 有同样的问题
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多