有什么方法可以在查询中引用参数的属性吗?

Any way to reference an attribute of a parameter in the query?

是否有任何方法可以引用作为 JPA 存储库查询一部分的参数的属性?

我的样本是

@Entity
public class Matchday implements Serializable {
    @Id
    private int matchdayNumber;
    //..
    //setters and getters defined
    //..
    //hashCode and equals methods overridden
}

public interface MyRepository extends JpaRepository<Matchday, Integer> {
    @Query("... WHERE t.matchday.matchdayNumber < :matchday.matchdayNumber - 1;")
    public findByCriteria(Matchday matchday);
}

结构 :matchday.matchdayNumber 似乎不是有效的语法。除了将 matchdayNumberint 值而不是对 Matchday 对象的引用传递给此方法之外,还有其他方法吗?

看起来这可以通过 Spring JPA Data 实现,它允许在查询中使用 SpEL。

public interface MyRepository extends JpaRepository<Matchday, Integer> {
    @Query("... WHERE t.matchday.matchdayNumber < :#{#matchday.matchdayNumber - 1}")
    public findByCriteria(Matchday matchday);
}