1

我有这个 SQL 命令来搜索特定日期内的可用房间:

select k.nr from room k 
where not  exists (select 'x' from room_reserveration as kr 
INNER JOIN reserveration as r ON (r.reserveration_id = kr.reserveration_id) 
INNER JOIN hotel as h ON (h.hotel_id = r.hotel_id) 
where k.room_id = kr.room_id AND (r.begin_date between "2012-06-18" and "2012-06-27" ) and  (r.end_date between "2012-06-18" and "2012-06-27" ) 
)

我试图转换为 HQL:

session.createQuery("select k.nr from room k  where not  exists 
(select 'x' from RoomReserveration  kr, Reserveration  r   
join kr.Reserveration  join    k.hotel     join  kr.room      
where (r.begin_date between '2012-06-18' and '2012-06-27' ) and  
(r.eind_date between '2012-06-18' and '2012-06-27' ) )");

如果我编译我的 java 代码,我会得到这个输出:

Hibernate: select Room0_.nr as col_0_0_ from Room Room0_ where  not (exists (select 'x' from Room_reservation Roomreser1_ inner join reservation reservation3_ on Roomreser1_.reservation_id=reservation3_.reservation_id inner join Room Room5_ on Roomreser1_.Room_id=Room5_.Room_id cross join reservation reservation2_ where (reservation2_.begin_date between '2012-06-18' and '2012-06-27') and (reservation2_.eind_date between '2012-06-18' and '2012-06-27')))

为什么在这里添加交叉连接而没有结果?

映射:

预订房间:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="model.RoomReservation" table="room_reservation">
        <id name="room_reservation_id">
            <generator class="identity"/>
        </id>
        <property name="details"/>
        <many-to-one name="reservation" column="reservation_id" />
        <many-to-one name="room" column="room_id" />
    </class>
</hibernate-mapping>

预订

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="model.Reservation" table="reservation">
        <id name="reservation_id">
            <generator class="identity"/>
        </id>
        <property name="date_reservation" type="java.util.Date"/>
        <property name="begin_date" type="java.util.Date"/>
        <property name="eind_date" type="java.util.Date"/>

        <set name="roomReservation"  inverse="true">
            <key column="reservation_id"/>
            <one-to-many class="model.RoomReservation"/>
        </set>
        <many-to-one name="hotel" column="hotel_id" />
    </class>
</hibernate-mapping>

房间

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="model.Room" table="room">
        <id name="room_id">
            <generator class="identity"/>
        </id>
        <property name="nr"/>
        <set name="facilities"  inverse="true">
            <key column="room_id"/>
            <one-to-many class="model.RoomFacilities"/>
        </set>
        <many-to-one name="hotel" column="hotel_id" />
        <many-to-one name="roomType" column="room_type_id" cascade="save-update" />
    </class>
</hibernate-mapping>

酒店:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="model.Hotel" table="hotel">
        <id name="hotel_id">
            <generator class="identity"/>
        </id>
        <property name="name"/>  
        <set name="rooms">
            <key column="hotel_id"/>
            <one-to-many class="model.Room"/>
        </set>
    </class>
</hibernate-mapping>
4

1 回答 1

1

交叉连接来自这样一个事实,即,您正在与

 select 'x' from RoomReserveration  kr, Reserveration  r

查询应该是:

select room.nr from Room room where not exists 
(select 'x' from RoomReservation roomReservation   
 join roomReservation.reservation reservation 
 join reservation.hotel hotel
 where (reservation.begin_date between '2012-06-18' and '2012-06-27') 
 and (reservation.eind_date between '2012-06-18' and '2012-06-27')
 and roomReservation.room = room)

请注意,加入酒店没有任何目的,除了过滤没有任何酒店的预订。

于 2012-07-04T16:49:23.940 回答