Java 在同一存储库中创建JPA投影
我想创建这个JPA投影:Java 在同一存储库中创建JPA投影,java,jpa,spring-data-jpa,projection,jpa-2.1,Java,Jpa,Spring Data Jpa,Projection,Jpa 2.1,我想创建这个JPA投影: @Repository public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> { @Query(value = "SELECT count(id) as count, status, error_clas
@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);
}
public interface PaymentTransactionsDeclineReasonsDTO {
int getId();
int getCount();
//... and so on
}
但我有例外
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]
你知道我如何解决这个问题吗?我想我必须创建一个单独的存储库扩展JpaRepository
但我希望使用相同的存储库,因为我有使用适当实体的查询。有什么解决方案吗?您应该能够使用普通投影作为界面来实现这一点。 您可以找到一个关于如何设置基于界面的投影的好教程。 基本上,您可以将
PaymentTransactionDeclineReasonsTo
转换为一个接口,并声明要通过投影访问的getter:
@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);
}
public interface PaymentTransactionsDeclineReasonsDTO {
int getId();
int getCount();
//... and so on
}
这样,您仍然可以使用相同的存储库,但只能获取实际类的选定属性