2013-03-28 60 views
1

我試圖設置一個不會自動生成的自定義@Query,但無論我嘗試什麼,都試圖將方法名稱中的屬性與我的屬性進行匹配返回對象。Spring Data - Customer @Query沒有屬性匹配

如何在不嘗試構建查詢的情況下通過此查詢來運行並失敗並出現org.springframework.data.mapping.PropertyReferenceException?也許@Query是錯誤的註釋?

我的回購目前看起來是這樣的:當它不能找到匹配的「當前」場在我的時間表類中發生

@Repository 
public interface ScheduleRepository extends CrudRepository<Schedule, Integer> 
{ 
    @Query 
    List<Schedule> findByTypeAndAirDateBetweenOrderByAirDateDesc(String type, Date startDate, Date endDate); 

    @Query("SELECT s FROM Schedule s WHERE s.type = 'A' AND (s.airDate BETWEEN :startDate AND :endDate) ORDER BY ABS(DATEDIFF(s.airDate, NOW())) LIMIT 1") 
    List<Schedule> findCurrentAd(Date startDate, Date endDate); 
} 

的異常。我不想要它。我只是希望它能夠按照我的定義來運行查詢,而不會問任何問題。如你所見,我是Spring MVC & Data的新手。

感謝任何人的幫助!

+0

我的回答是否解決了您的問題@Dave V? –

回答

0

如果您使用綁定參數,如:VALUE,則應使用@Param("value")來匹配您的@Query註釋。試試這個:

@Repository 
public interface ScheduleRepository extends CrudRepository<Schedule, Integer> 
{ 
    @Query 
    List<Schedule> findByTypeAndAirDateBetweenOrderByAirDateDesc(String type, Date startDate, Date endDate); 

    @Query("SELECT s FROM Schedule s WHERE s.type = 'A' AND (s.airDate BETWEEN :startDate AND :endDate) ORDER BY ABS(DATEDIFF(s.airDate, NOW())) LIMIT 1") 
    List<Schedule> findCurrentAd(@Param("startDate") Date startDate, @Param("endDate") Date endDate); 
} 

如果您選擇使用沒有@Param註釋構造函數中,你可以使用?n像這樣綁定:

@Repository 
public interface ScheduleRepository extends CrudRepository<Schedule, Integer> 
{ 
    @Query 
    List<Schedule> findByTypeAndAirDateBetweenOrderByAirDateDesc(String type, Date startDate, Date endDate); 

    @Query("SELECT s FROM Schedule s WHERE s.type = 'A' AND (s.airDate BETWEEN ?1 AND ?2) ORDER BY ABS(DATEDIFF(s.airDate, NOW())) LIMIT 1") 
    List<Schedule> findCurrentAd(Date startDate, Date endDate); 
} 

n個結合代表了你的方法參數的順序? ?1 = startDate,?2 = endDate。

參考:Spring Docs

相關問題