1

自動的に生成されないカスタム @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);
}

Schedule クラスで一致する「現在の」フィールドが見つからない場合、例外が発生します。私はそれをしたくありません。定義したとおりにクエリを実行したいだけで、質問はありません。

お気づきかもしれませんが、Spring MVC と Data は初めてです。

誰の助けにも感謝します!

4

1 に答える 1

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 = 開始日、?2 = 終了日。

参考:Spring Docs

于 2013-03-28T16:15:25.250 に答える