使用条件AND NOT EXISTS将SQL转换为HQL

时间:2012-09-11 23:34:34

标签: java spring hibernate nhibernate-mapping hql

我需要转换这个SQL语句HQL,但我认为NOT EXISTS不能在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

1 个答案:

答案 0 :(得分:4)

不幸的是,你没有提供有关你的域名模型的任何信息,所以我们必须在这里作出一些假设...首先,我不考虑医生和时间表之间的任何映射关联,尽管你可能想要映射它。根据好的设计我使用参数而不是文字。我假设所有引用的表都已映射,并且正在为类使用“逻辑名称映射”。最后,我使用您的列名作为域模型属性名称...

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