简体   繁体   English

在同一存储库中创建JPA Projection

[英]Create JPA Projection into the same repository

I want to create this JPA Projection: 我要创建此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);
}

Class based Projection DTO: 基于类的投影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;

}

But I get exception 但是我有例外

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]

Do you know how I can fix this issue? 您知道如何解决此问题吗? I suppose that I have to create a separate repository extends JpaRepository<PaymentTransactionsDeclineReasonsDTO, Integer> ? 我想我必须创建一个extends JpaRepository<PaymentTransactionsDeclineReasonsDTO, Integer>的单独的存储库?

But I would like to use the same repository because I have queries which use the proper entity. 但是我想使用相同的存储库,因为我有使用适当实体的查询。 Is there some solution? 有什么解决办法吗?

You should be able to do this with a normal projection as an interface. 您应该能够使用普通投影作为界面来执行此操作。 Here you can find a good tutorial of how to set up an interface based projection. 在这里,您可以找到有关如何设置基于接口的投影的很好的教程。 Basically you would convert your PaymentTransactionsDeclineReasonsDTO to an interface and declare the getters you want to access through your projection: 基本上,您可以将PaymentTransactionsDeclineReasonsDTO转换为接口,并声明要通过投影访问的getter:

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

This way you can still use the same repository but only get selected properties of your actual class. 这样,您仍然可以使用相同的存储库,但只能获取实际类的选定属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM