I am using Spring Data JPA and trying to add a query to my repository. I was trying to just build the query without the @Query annotation like:
List-
I wanted to do the same thing with getting all data for a particular station, that was between two dates.
findByStartdateBetweenOrEnddateBetweenAndStation(start,end,start,end,station)
Gives you ==>
SELECT...WHERE (startdate between start,end) OR ( enddatebetween start,end AND station=station)
And:
findByStationAndStartdateBetweenOrEnddateBetween(station, start,end,start,end)
Gives you ==>
SELECT...WHERE (station=station AND startdate between start,end) OR enddatebetween start,end
Both are wrong as I wanted:
SELECT...WHERE station=station AND (startdate between start,end OR enddatebetween start,end)
Use the @Query to be very specific and don't take chances mixing your AND/OR with the method name.
Well, you are very close. To do this without @Query
the In
or IsIn
keyword can be used(not sure whether these keywords were supported at the time the question was asked):
List<Item> findByTypeAndStateInAndStartDateBetween(Type type, Collection<State> states, Date startDate, Date endDate);
In and NotIn also take any subclass of Collection as parameter as well as arrays or varargs. For other syntactical versions of the very same logical operator check Repository query keywords.
This will suffice your need of passing any number of states for the OR criteria.
Reference: Table 4. Supported keywords inside method names in Query creation docs