以下 SQL 查询:
select distinct hotel.country 'Country', hotel.id 'Hotel ID', hotel.Name'Name', room.id 'Room ID'
from room, stay, reservation, hotel
where
(stay.roomid = room.id)
and (stay.reservationid = reservation.id)
and (reservation.status != 'Booked' AND reservation.status != 'CheckedIn')
and (reservation.arrivaldate >= '2012-08-08')
and (reservation.leavedate <= '2012-08-15')
and (room.hotelid = hotel.id)
order by hotel.country, hotel.id, hotel.name, room.id asc
这将为我提供每个国家/地区每家酒店的可用客房列表。现在我必须把它放在 HQL 中,但因为这个我不能这样做:
Query query = session.createQuery("from room, stay, reservation, hotel where stay.roomid = room.id");
这不起作用,因为stay.roomid
它是一个 Room 对象,而room.id
它只是一个整数。我对 Hibernate/HQL 很陌生,参考手册并没有真正带我去任何地方......我如何“转换”(可怕的词,我知道这不涉及实际转换)这个 SLQ 查询到 HQL 语句? 谢谢。
更新
使用连接会导致同样的问题
Query query = session.createQuery("" +
"from Stay stay " +
"join stay.ReservationID reservation " +
"join stay.RoomID room " +
"join room.HotelID hotel " +
"where (stay.RoomID = room.ID)");
我真的在这里错过了一些东西(可能非常合乎逻辑)......