0

我希望将以下 SQL 转换为 JPQL:

SELECT  *
FROM    rooms
WHERE   NOT EXISTS
(        
    SELECT  *
    FROM    booking, booking_has_rooms
    WHERE   rooms.number=booking_has_rooms.rooms_number AND 
            booking.booking_id=booking_has_rooms.booking_booking_id AND 
            :date BETWEEN booking.checkin AND booking.checkout
);

到目前为止,我有这个:

SELECT rooms 
FROM Rooms rooms 
WHERE NOT EXISTS 
(
    SELECT bk 
    FROM Booking b 
    JOIN  b.roomsCollection bk 
    WHERE :date BETWEEN b.checkin AND b.checkout
)

如果在指定日期没有预订,它将正确返回所有房间。但是,如果任何预订在指定日期 - 它根本不会返回任何房间。

4

1 回答 1

1

您在内部查询中缺少一个子句:

SELECT rooms 
FROM Rooms rooms 
WHERE NOT EXISTS 
(
    SELECT bk 
    FROM Booking b 
    JOIN  b.roomsCollection bk 
    WHERE :date BETWEEN b.checkin AND b.checkout
    and bk.id = rooms.id
)

如果您将房间重命名为房间,您的代码会更清晰。每个实例都是一个房间,所以它应该使用单数形式。

于 2013-02-02T20:16:36.237 回答