0
@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'"

crazy hoe
  • 1
  • 1

1 Answers1

0

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);
  • Thank you, I tried your suggestion and did with 2 queries, though my job is done still I didn't like the fact we can't ran custom queries like with jdbc :(. Also Max is not exist and I did with orderby and top. – crazy hoe Jul 06 '23 at 18:33