java - 使用 hibernate native 查询的错误 BigInteger 结果

标签 java sql hibernate postgresql

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

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 手动执行的 native 查询返回正确的值。

我已经尝试检索结果作为对象列表,但结果是一样的

最佳答案

我已经找到了足够的解决方法

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

关于java - 使用 hibernate native 查询的错误 BigInteger 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726380/

相关文章:

java - 在 SimpleDateFormat() 中更改时区

mysql - 使用 MySQL,我如何知道谁每年至少参加一次事件?

c# - 从 Oracle DB 中选择所有表名

mysql - 获取用户未对 SQL 查询投票的所有配置文件

java - org.hibernate.HibernateException : Could not parse configuration: hibernate. cfg.xml

java - 即使我们在hibernate中设置了自动增量,mysql中的表是否也必须具有自动增量属性

java - Spring , hibernate : issue with creating new @Entity model

java - sqlite中的超时异常

java - 用 HTML 标签解析 CDATA 并获取内容

java - 如何在 Hibernate 中将字符串映射到数据库序列