Spring JPA :: No converter found capable of converting from type

后端 未结 6 1754
有刺的猬
有刺的猬 2021-02-03 23:59

I am using spring with JPA and trying to execute query using @query with SQL query.and trying to map the result to an object. I have different entity class and mapping to other

6条回答
  •  孤独总比滥情好
    2021-02-04 00:33

    If you are still looking for an answer, here is how I did

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class StatsDTO {
        private Integer userCount;
        private Byte typeId;
        private Instant modifiedAt;
    }
    

    and your query should be like this

    public interface UserRepository extends CrudRepository {
        @Query(value = "select new com.example.package.StatsDTO(count(type_id) userCount, typeId, modifiedAt from "
                    + "UserCampaignObjective where campId = ?1 group by objectiveTypeId,modifiedAt")
    
            List getStatsDTO(Long camp_id);
    }
    

    make sure you have a constructor present in StatsDTO to map all fields.

提交回复
热议问题