2012-09-11 69 views
0

我需要轉換此SQL語句HQL,但我認爲不存在不在HQL中工作 請幫助我!使用標準和不存在將SQL轉換爲HQL

SELECT doctor.idUser, schedule.idSchedule, schedule.timeStart, schedule.day 
FROM doctor, schedule 
    WHERE schedule.day='LUNES' 
     AND schedule.timeStart > '08:00:00' 
     AND doctor.idUser= '1' 
     AND doctor.idUser = schedule.idUserDoctor 
AND NOT EXISTS(SELECT * FROM appointment 
    WHERE schedule.idSchedule = appointment.idSchedule 
     AND doctor.idUser = schedule.idUserDoctor 
     AND appointment.appointmentDate ='2012-09-06') 
AND NOT EXISTS (SELECT * FROM temporaryschedule 
     WHERE schedule.idSchedule = temporaryschedule.idSchedule 
     AND doctor.idUser = schedule.idUserDoctor" 
     AND temporaryschedule.appointmentDate='201-09-06') 
ORDER BY schedule.timeStart ASC 
+0

關於'不EXISTS'這個[問題](http://stackoverflow.com/questions/3672444/where-exists-in- hibernate-hql)可能會有所幫助。你將不得不從你的'NOT EXISTS'子句中單獨確定對象(或它們的ID),並像這樣插入它們:'AND doctor.idUser NOT IN(1,2,3)'。 – Pao

回答

4

可惜你沒有提供有關您的域模型的任何信息,所以我們要在這裏做一個比較少的假設。首先,我不佔醫生和進度之間的任何映射的關聯,雖然你」 d可能想要映射它。根據優秀的設計,我使用的參數是文字。我假設所有被引用的表都被映射,並且正在使用這些類的「邏輯名映射」。最後,我使用的列名作爲域模型的屬性名稱...

select ... 
from Doctor d, Schedule s 
where s.day = :day 
    and s.timeStart > :startTime 
    and d.idUser = :doctorId 
    and d.idUser = s.idUserDoctor 
    and not exists (
     select * 
     from Appointment appt 
     where s.idSchedule = appt.idSchedule 
     and d.idUser = s.idUserDoctor 
     and apt.appointmentDate = :apptDate 
) 
    and not exists (
     select * 
     from TemporarySchedule ts 
     where s.idSchedule = ts.idSchedule 
     and d.idUser = s.idUserDoctor 
     and ts.appointmentDate = tempSchedDate 
) 
order by s.startTime asc