gpt4 book ai didi

java - 只有来自自定义查询的最后一条记录

转载 作者:行者123 更新时间:2023-11-29 04:16:35 24 4
gpt4 key购买 nike

以下查询返回一个列表,但我只对列表的最后一个元素感兴趣。

@Query("SELECT r FROM Reservation r WHERE r.reservationSeance.id=:seanceId AND r.seanceDate=:seanceDate")
public Reservation findReservationBySeanceDateAndSeanceId(@Param("seanceId") int seanceId, @Param("seanceDate") java.time.LocalDate seanceDate);

我应该如何重写 SQL 查询以实现我的想法?

最佳答案

一种可能的解决方案是使用 ORDER BY r.id DESC :

@Query("SELECT r FROM Reservation r  " +
"WHERE r.reservationSeance.id=:seanceId AND r.seanceDate=:seanceDate " +
"ORDER BY r.id DESC")
public Reservation findReservationBySeanceDateAndSeanceId(
@Param("seanceId") int seanceId,
@Param("seanceDate") java.time.LocalDate seanceDate, Pageable pageable);

并且由于在 JPQL 中无法使用限制,您可以使用 Pageable

Pageable pageable = new PageRequest(0, 1);
Reservation reservation = r.findReservationBySeanceDateAndSeanceId(seanceId, seanceDate, pageable);

没有查询的另一种可能的解决方案:

public Reservation findTop1ByReservationSeanceAndSeanceDateOrderByIdDesc(
ReservationSeanceEntity reservationSenace,
java.time.LocalDate seanceDate
)

在第二个解决方案中,您必须传递 ReservationSeance 对象而不是 ReservationSeanceid,查询可以读作:

Find top 1 (first one) by `ReservationSeance` and `SeanceDate` order by `Id` Desc order

关于java - 只有来自自定义查询的最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com