【问题标题】:Transform SQL to HQL with criteria AND NOT EXISTS使用条件 AND NOT EXISTS 将 SQL 转换为 HQL
【发布时间】:2023-03-08 23:18:01
【问题描述】:

我需要将此 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

【问题讨论】:

  • 关于NOT EXISTS 这个question 可能会有所帮助。您必须分别从 NOT EXISTS 子句中确定对象(或它们的 ID),然后像这样插入它们:AND doctor.idUser NOT IN (1,2,3)

标签: java spring hibernate nhibernate-mapping hql


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-12-29
    • 2017-01-08
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多