2012-07-04 20 views
1

我有一個特定日期內搜索可用客房SQL命令:HQL:CROSS JOIN

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'))) 

爲什麼這是在這裏添加一個交叉連接而沒有結果?

映射:

RoomReservation:

<?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> 
+0

我不認爲HQL連接意味着與SQL連接相同http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-joins。實體映射將有助於爲解決此問題提供一些幫助。 – gkamal

回答

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) 

注意,聯同酒店除了tofilter沒有任何酒店的預訂外,沒有任何用途。