Spring Data Jpa @NamedNativeQuery和@SqlResultSetMapping:未找到能够从类型[$TupleConverter$TupleBackedMap]进行转换的转换器

xjreopfe  于 8个月前  发布在  Spring
关注(0)|答案(1)|浏览(106)

我得到了以下stacktrace:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.test.repository.models.customs.LightAssetWithQtyDto]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-6.0.9.jar:6.0.9]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-6.0.9.jar:6.0.9]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-6.0.9.jar:6.0.9]
[...]

我的类如下:

AssetEntity

@SqlResultSetMapping(
        name = "LightAssetWithQtyDtoMapping",
        classes = {@ConstructorResult(
                targetClass = LightAssetWithQtyDto.class,
                columns = {
                        @ColumnResult(name = "id", type = Integer.class),
                        @ColumnResult(name = "stock_symbol", type = String.class),
                        @ColumnResult(name = "total", type = BigDecimal.class)
                })}
)
@NamedNativeQueries(
        @NamedNativeQuery(name = "AssetEntity.findAllAssetsWithQty",
                resultSetMapping = "LightAssetWithQtyDtoMapping",
                query = "SELECT a.id, a.stock_symbol, sum(t.quantity) AS total " +
                        "FROM asset a " +
                        "RIGHT JOIN \"transaction\" t ON a.id = t.asset_id " +
                        "GROUP BY a.stock_symbol, a.id HAVING sum(t.quantity) > 0"
        )
)
@Getter
@Setter
@Entity
@Table(name = "asset")
public class AssetEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    [...]

LightAssetWithQtyDto

@Getter
@Setter
public class LightAssetWithQtyDto {
    private Integer id;

    private String stockSymbol;

    private BigDecimal total;

    public LightAssetWithQtyDto(Integer id, String stockSymbol, BigDecimal total) {
        this.id = id;
        this.stockSymbol = stockSymbol;
        this.total = total;
    }
}

AssetRepository

@Repository
public interface AssetRepository extends CrudRepository<AssetEntity, Integer> {
    List<LightAssetWithQtyDto> findAllAssetsWithQty();
}

@ConstructorResult似乎与LightAssetWithQtyDto构造函数匹配。有谁知道这个错误可能是从哪里来的??
我希望findAllAssetsWithQty返回一个List。相反,我得到了一个例外。

xvw2m8pv

xvw2m8pv1#

@Repository
public interface AssetRepository extends CrudRepository<AssetEntity, Integer> {
    @Query(name = "AssetEntity.findAllAssetsWithQty", nativeQuery = true)
    List<LightAssetWithQtyDto> findAllAssetsWithQty();
}

相关问题