如何实现JPQL查询?

How to implement JPQL query?

我在 PostgreSQL 中有工作查询:

select s.id, s.seat_number as available_seat, s.row_number as available_row, rm.room_name as screening_room
from seats s
         join rooms rm on rm.id=s.room_id
         left join (
    select r.seat_id from reserved_seats r
                              join reservations  res on res.id=r.reservation_id AND res.screening_id = 3 ) res on res.seat_id=s.id
where res.seat_id is null AND s.room_id=3
ORDER BY s.id;

但是我在将它翻译成 JPA 查询语言时出错了。

我可以在 JPQL 中使用嵌套的 SELECT 吗?

答案是使用原生查询:

@Query(value =
            "SELECT s.id seatId, s.seat_number availableSeat, " +
                    "s.row_number availableRow, rm.name screeningRoom \n" +
                    "FROM seats s\n" +
                    "JOIN rooms rm on rm.id=s.room_id\n" +
                    "   LEFT JOIN (\n" +
                    "       SELECT r.seat_id FROM reserved_seats r\n" +
                    "       JOIN reservations res ON res.id=r.reservation_id " +
                    "       AND res.screening_id = :screeningId) res ON res.seat_id=s.id\n" +
                    "WHERE res.seat_id IS NULL AND s.room_id=:roomId AND s.row_number=:rowNumber\n" +
                    "ORDER BY s.id;", nativeQuery = true)

答案是使用原生查询:

@Query(value =
            "SELECT s.id seatId, s.seat_number availableSeat, " +
                    "s.row_number availableRow, rm.name screeningRoom \n" +
                    "FROM seats s\n" +
                    "JOIN rooms rm on rm.id=s.room_id\n" +
                    "   LEFT JOIN (\n" +
                    "       SELECT r.seat_id FROM reserved_seats r\n" +
                    "       JOIN reservations res ON res.id=r.reservation_id " +
                    "       AND res.screening_id = :screeningId) res ON res.seat_id=s.id\n" +
                    "WHERE res.seat_id IS NULL AND s.room_id=:roomId AND s.row_number=:rowNumber\n" +
                    "ORDER BY s.id;", nativeQuery = true)