Bug in Spring Data JPA: Spring Data returns List<BigInteger> instead of List<Long>

This is a issue with Spring data JPA. If in DB the datatype is defined as BigInteger and in JPA query we tries to fetch as Long then it will not give any error , but it set value as BigInteger in Long datatype.

Solutions:

  1. Use BigInteger as return type

    @Query(value = "select distinct(oid) from unit", nativeQuery = true) List<BigInteger> testMethod();

    then set the variable as below.
    Long variable = bigIntegerValue.longValue();

  2. Use String as return Type and convert to Long

    @Query(value = "select distinct(oid) from unit", nativeQuery = true) List<String> testMethod();

    then set the value as

    Long variable = Long.valueOf(stringValue);

  3. Change DB column type to Integer/Number.

  4. Get the Value from Entity Object.

    Long variable = dpConfigData.getOid();

    where dpConfigData is object of Entity(DpConfigData.class)