Spring JPA Data “OR” query

后端 未结 2 1815
甜味超标
甜味超标 2021-02-18 23:02

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         


        
2条回答
  •  迷失自我
    2021-02-18 23:46

    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.

提交回复
热议问题