【问题标题】:No converter found capable of converting from type找不到能够从类型转换的转换器
【发布时间】:2021-11-19 01:09:39
【问题描述】:

我有这门课:

public class UsersXCountry {

    Integer numUsers;
    String countryCode;

}

这个查询:

@Query(value = "select count('1'), country_code from t_user group by country_code",
        nativeQuery = true)
List<UsersXCountry> usersXCountry();

但我有这个错误

No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.losmundo.backend.domain.UsersXCountry]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322)

【问题讨论】:

  • UsersXCountry 不是 Spring JPA 类?
  • 您无法使用本机查询将单个列直接选择到自定义对象类中。如果您想这样做,请使用interface 而不是class,或使用@NamedNativeQuery@SqlResultSetMapping 将结果映射到自定义对象类。

标签: java spring-boot jpa spring-data-jpa


【解决方案1】:

除了将 getter、setter 添加到 UsersXCountry

之外,名称必须相同
@Query(value = "select count('1') as numUsers, country_code as countryCode from t_user group by country_code",
        nativeQuery = true)
List<UsersXCountry> usersXCountry();

【讨论】:

    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2020-05-04
    • 2019-03-25
    • 2020-10-14
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多