Search code examples
jpaspring-data-jpahqljpql

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


Is there any way to reference an attribute of a parameter that is part of a query for a JPA repository?

My sample is

@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);
}

The construction :matchday.matchdayNumber does not seem to be a valid syntax. Is there any other way to do it than passing the int value for matchdayNumber instead of a reference to Matchday object to this method?


Solution

  • Looks like this is possible with Spring JPA Data which allows SpEL in queries.

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