crazy hoe
crazy hoe

Reputation: 11

@Query is not working with r2dbc and postgres

@Repository
public interface XYZRepository extends R2dbcRepository<XYZ, XYZPK> {

    @Query(value = "select * from xyz\n" +
            "right join (select max(capture_date) capture_date from xyz) as t using (capture_date)\n" +
            "where id = :id", nativeQuery = true)
    Flux<XYZ> getLatestById(@Param("id") String id);

}

I was expecting it to return just latest from database but it returning all entries by id and I tried renaming it to "getLatestXYZ" then getting error "No property 'getLatestXYZ' found for type 'XYZ'"

Upvotes: 1

Views: 592

Answers (1)

Gaurav Bhatia
Gaurav Bhatia

Reputation: 111

There is no query method which parses getLatest. You can use getFirst or getTop to sort the results and get top results (by asc or desc order). For example, findTopByIdAndOrderByCaptureDateDesc(Long id) . This will only filter the rows with id and get row with max Capture Date. If we need to filter the row with max(capture_Date) then @Query option can be used as you showed or shown below. Here method name will not be parsed as Query annotation takes priority.

@Query(value = "SELECT * FROM xyz  WHERE m.id = :id AND m.capture_date= (SELECT MAX(e.capture_date) FROM xyz e)", nativeQuery = true)
Optional<XYZ> findByIdWithMaxCaptureDate(@Param("id") Long id);

Upvotes: 0

Related Questions