javaspringspring-bootspring-data-jpajpql

No converter found capable of converting from Integer type to CustomType entity


I am getting the following stacktrace:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.train.management.entities.TrainDetails]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:294) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:185) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.core.convert.support.ArrayToObjectConverter.convert(ArrayToObjectConverter.java:68) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:182) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:165) ~[spring-core-6.1.6.jar:6.1.6]
    at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:305) ~[spring-data-commons-3.2.5.jar:3.2.5]
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:233) ~[spring-data-commons-3.2.5.jar:3.2.5]
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:240) ~[spring-data-commons-3.2.5.jar:3.2.5]
    at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:160) ~[spring-data-commons-3.2.5.jar:3.2.5]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:152) ~[spring-data-jpa-3.2.5.jar:3.2.5]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:137) ~[spring-data-jpa-3.2.5.jar:3.2.5]
    at 

My classes are the following

TrainDetails


@Entity
@Table(name = "train_details")
public class TrainDetails {
    
    @Id
    @Column(name = "train_id")
    private String id;
    
    @Column(name = "train_name")
    private String trainName;
    
    @Column(name = "total_distance_covers")
    private String totalDistance;
    
    @Column(name = "train_code")
    private String trainCode;
    
    @Column(name = "total_stops")
    private int totalStops;
    
    @ManyToOne
    @JoinColumn(name = "start_station")
    private Station startStation;
    
    
    @ManyToOne
    @JoinColumn(name = "end_station")
    private Station endStation;

    @OneToMany(mappedBy = "trainName", cascade = CascadeType.ALL)
    private List<Coach> coaches;

    
}

My repo interface is

StationRepo


public interface StationRepo extends JpaRepository<Station, String> {

    
    @Query(nativeQuery = true, value="SELECT td.*\r\n"
            + "FROM train_details td\r\n"
            + "JOIN station s_start ON td.start_station = s_start.station_id\r\n"
            + "JOIN station s_end ON td.end_station = s_end.station_id\r\n"
            + "WHERE s_start.station_code = ?1"
            + " OR s_end.station_code = ?1")
    public List<TrainDetails> getAllTrainsComingOnStation(String stationCode);

}

I tried to change query to this also

    @Query("SELECT td FROM TrainDetails td\r\n"
            + "JOIN td.startStation s_start\r\n"
            + "JOIN td.endStation s_end\r\n"
            + "WHERE s_start.stationCode = :stationCode\r\n"
            + "   OR s_end.stationCode = :stationCode\r\n")
    public List<TrainDetails> getAllTrainsComingOnStation(@Param("stationCode") String stationCode);
    

it was working perfectly fine the doubt i have is why it is able to work for second but not native query one? spring boot 3.2.5 version is what i am using currently


Solution

  • Actually I have found answer my code's return type which i was mentioning List was wrong it is actually returning List so that's why i was getting exception.

    @Query(nativeQuery = true, value="SELECT td.*\r\n"
            + "FROM train_details td\r\n"
            + "JOIN station s_start ON td.start_station = s_start.station_id\r\n"
            + "JOIN station s_end ON td.end_station = s_end.station_id\r\n"
            + "WHERE s_start.station_code = :stationCode"
            + " OR s_end.station_code = :stationCode")
    public Object[] getAllTrainsComingOnStation(@Param("stationCode") String stationCode);