簡體   English   中英

實現一個spring數據倉庫接口中的常規接口

[英]Implement a regular interface in a spring data repository interface

我正在使用一個擴展 spring 數據 JpaRepository 的存儲庫,並想讓它擴展另一個接口。

以前,我的數據庫存儲庫如下所示:

interface PublicTransportPricingZoneRepository : JpaRepository<PublicTransportPricingZone, Long> {

}

我現在創建了另一個接口TransitTicketRepo ,定義如下

interface TransitTicketRepo {
    fun findPossibleTickets(geometry: Geometry): Collection<TransitTicket>
}

現在想使用PublicTransportPricingZoneRepository中的默認方法實現接口。 我試圖通過將 PublicTransportPricingZoneRepository 的代碼更改為來解決這個問題

interface PublicTransportPricingZoneRepository : JpaRepository<PublicTransportPricingZone, Long>, TransitTicketRepo {
    fun findPossibleTickets(geometry: Geometry): Collection<TransitTicket> {
       // do something
       return emptyList()
    } 
}

但是在啟動應用程序時收到以下錯誤消息。

org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Collection PublicTransportPricingZoneRepository.findPossibleTickets(Geometry); Reason: Failed to create query for method public abstract java.util.Collection...

我假設解決方案是以某種方式告訴 spring 數據停止自動生成findPossibleTickets的查詢,但一直無法找出如何。

你可以這樣

簡而言之。:

@Repository
// Your bean that will be glued together by spring
interface PublicTransportPricingZoneRepository extends JpaRepository<...>, PublicTransportPricingZoneCustomRepository {
  // Other methods, but everything must be annotated with @Query or possible for spring to guess what it needs to do from its name (google derived methods)
}

interface PublicTransportPricingZoneCustomRepository {
  // define custom methods
  List<String> nameIsNotImportant();
}

@Service
// naming here is important - it must be names as interface + Impl, otherwise spring won't pick it up
// apart from that, it's a regular bean - you can Autowire, etc
class PublicTransportPricingZoneCustomRepositoryImpl implementats PublicTransportPricingZoneCustomRepository {
  // Implement custom methods
  @Overridden
  public List<String> nameIsNotImportant() {
    // impl
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM