2013-03-28 62 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

相关问题