Home > Software engineering >  How to work around java.math.BigInteger cannot be cast to java.lang.Integer?
How to work around java.math.BigInteger cannot be cast to java.lang.Integer?

Time:12-16

I have a problem with Spring-boot, my request repository is written like that:

package com.ftm.webappli.repository.population;

import java.util.List;

import com.ftm.webappli.model.population.All;
import com.ftm.webappli.model.population.MenWomen;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface AllRepository extends JpaRepository<All, Long> {

    @Query(
        value = "SELECT SUM(p15_pop) AS total_population, SUM(p15_poph) AS total_hommes, SUM(p15_popf) AS total_femmes FROM public.iris_pop_2015 WHERE com = :code",
        nativeQuery = true)
    List<MenWomen> sumPopulationHommeFemme(String code);
    
}

She return 3 BigInt on PostgreSQL.

In the method i've write :

package com.ftm.webappli.model.population;

import java.io.Serializable;
import java.math.BigInteger;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

@Entity
@FieldDefaults(level = AccessLevel.PRIVATE)
@Getter
@NoArgsConstructor
@Setter
@Table(schema = "public", name = "iris_pop_2015")
public class MenWomen implements Serializable {
    
    @Id
    @Column(name = "total_population")
    private BigInteger nbTotal;

    @Column(name = "total_hommes")
    private BigInteger nbMen;

    @Column(name = "total_femmes")
    private BigInteger nbWomen;
 
}

But i'v an error :

Failed to convert from type [java.lang.Object[]] to type [com.ftm.webappli.model.population.MenWomen] for value '{5766, 2767, 2999}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type

I've try to correct my request:

"SELECT SUM(p15_pop)::integer AS total_population, SUM(p15_poph)::integer AS total_hommes, SUM(p15_popf)::integer AS total_femmes FROM public.iris_pop_2015 WHERE com = :code"

And the Model to Integer, ex:

@Column(name = "total_hommes")
private Integer nbMen;

But doesn't work... The request work on PostgreSQL but not on Spring whith the '::integer'

I've trys also to correct my java.lang:

@Repository
public interface AllRepository extends JpaRepository<All, Integer>

Or

@Repository
public interface AllRepository extends JpaRepository<All, Long>

Or

@Repository
public interface AllRepository extends JpaRepository<All, BigInteger>

But doesn't work... You have an idea ?

CodePudding user response:

Please replace All with entity name i.e, MenWomen something like below,

@Repository
public interface AllRepository extends JpaRepository<MenWomen, Integer>
  • Related