简体   繁体   中英

Wrong BigInteger result using hibernate native query

I'm using Hibernate4 + PostgresSql. I need to get a list of ids from my DB without getting a whole object. 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! 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). Native query executed manually not through Hibernate returns correct values.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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