简体   繁体   中英

Why EntityManager createNativeQuery lists sometimes return a BigDecimal and other times an Integer?

This was baffling me a lot . I just figure out what was happening, but I'm going to ask and answer my own question for documentation in case it helps someone else.

I have the following code:

MyClass.java

    public List<Integer> 
    getPersonIdsByPrograms(Collection<Integer> programIds, boolean getFirst) {
      String sql = "SELECT p.PERSON_ID FROM PERSON p " + 
                   "WHERE p.PROGRAM_ID in (?programIds)";
      sql = sql.replaceAll("\\?programIds", StringUtils.join(programIds, ","));
      Query query = entityManager.createNativeQuery(sql);

      //EntityManager returns scalar values, in this case, BigDecimal
      List<BigDecimal> queryResults = query.getResultList();

      // !! This is the toggle that makes or breaks the tests !!
      // !! !! UPDATE: Red herring. This had no impact.  See answer for why !! !!
      if (getFirst) {
        Object firstObj = queryResults.get(0); //This makes the test pass!?!
        //This is always true
        System.out.println("Is firstObj a BigDecimal? Answer: " 
               + (firstObj instanceof BigDecimal));
      }

      //Returns list of BigDecimal, so need to convert to Integer
      List<Integer> result = Lists.transform(queryResults, new Function<BigDecimal, Integer>() {
        public Integer apply(BigDecimal n) {
            return Integer.valueOf(n.intValue());
        }
    });
    return result;
}

Warning Now here's where the answer comes in ans invalidates the test below, so take it with a grain of salt. I thought the first assertion was going to pass and the 2nd was going to fail. This was not the case: the first ended up failing in some circumstances.

MyClassTest.java

@Test
public void getPersonIdsByProgramsTest_BigDecimalOrInteger() {
    ArrayList<Integer> programs = Lists.newArrayList(131,141,161,162,0,113,110,26,5,28,50,58,51,29,121,31,101,41,27);
    List<?> personsByPrograms = userDao.getPersonIdsByPrograms(programs, true);
    TestingUtils.assertNotNullOrEmpty(personsByPrograms);
    Object shouldBeInteger = personsByPrograms.get(0);
    assertTrue(shouldBeInteger instanceof Integer);

    personsByPrograms = userDao.getPersonIdsByPrograms(programs, false);
    TestingUtils.assertNotNullOrEmpty(personsByPrograms);
    shouldBeInteger = personsByPrograms.get(0);
    assertTrue(shouldBeInteger instanceof Integer);
}

Answer: Different databases might return different scalar types.

My problem was the result of using a @RunWith(Parameterized.class) unit test that was running against 2 different databases (Oracle and MS SQL Server). It may have something to do with how the columns were defined (I haven't looked into it), but the basic gist is that you may not be able to rely on the specific object type that JPA will cast to when switching between databases. To be safe, you may just want to assume List<? extends Number> List<? extends Number> .

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