Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to work around java.math.BigInteger cannot be cast to java.lang.Integer?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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

@Repository
public interface AllRepository extends JpaRepository<MenWomen, Integer>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading