我要创建此JPA投影:

@Repository
public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {

    @Query(value = "SELECT count(id) as count, status, error_class, error_message, id FROM " +
            " payment_transactions " +
            " WHERE terminal_id = :id AND (created_at > :created_at) "      List<PaymentTransactionsDeclineReasonsDTO> transaction_decline_reasons(@Param("id") Integer transaction_unique_id, @Param("created_at") LocalDateTime created_at);
}


基于类的投影DTO:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class PaymentTransactionsDeclineReasonsDTO {

    private Integer id;

    private Integer count;

    private String status;

    private String error_class;

    private String error_message;

}


但是我有例外

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [org.plugin.service.PaymentTransactionsDeclineReasonsDTO]


您知道如何解决此问题吗?我想必须创建一个单独的存储库extends JpaRepository<PaymentTransactionsDeclineReasonsDTO, Integer>吗?

但是我想使用相同的存储库,因为我有使用适当实体的查询。有什么解决办法吗?

最佳答案

您应该能够使用普通投影作为界面来执行此操作。
Here您可以找到有关如何设置基于接口的投影的很好的教程。
基本上,您可以将PaymentTransactionsDeclineReasonsDTO转换为接口,并声明要通过投影访问的getter:

public interface PaymentTransactionsDeclineReasonsDTO {
    int getId();
    int getCount();
    //... and so on
}


这样,您仍然可以使用相同的存储库,但只能获取实际类的选定属性。

10-07 12:46
查看更多