如何将嵌套SQL转换为HQL

我是Hibernate和HQL的新手。 我想在HQL中编写更新查询,其SQL等效项如下:

update patient set `last_name` = "new_last", `first_name` = "new_first" where id = (select doctor_id from doctor where clinic_id = 22 and city = 'abc_city'); 

doctor_iddoctor PK, patient是FK和PK。 有一对一的映射。

相应的Java类是Patient(具有字段lastName,firstName,doctorId)和Doctor(具有字段doctorId)。

任何人都可以告诉我们上面的SQL查询的HQL等价物是什么?

非常感谢。

 String update = "update Patient p set p.last_name = :new_last, p.first_name = :new_first where p.id = some (select doctor.id from Doctor doctor where doctor.clinic_id = 22 and city = 'abc_city')"; 

如果检查规范,可以弄清楚如何短语hql查询。 您可以在那里找到有关子查询的部分。

我不认为你需要HQL(我知道,你明确地问过,但是既然你说你是Hibernate的新手,那么让我提供一个Hibernate风格的替代方案)。 我不喜欢HQL,因为你仍然处理字符串,这可能变得难以维护,就像SQL一样,并且你失去了类型安全性。

而是使用Hibernate条件查询和方法来查询您的数据。 根据您的类映射,您可以执行以下操作:

 List patients = session.CreateCriteria(typeof(Patient.class)) .createAlias("doctor", "dr") .add(Restrictions.Eq("dr.clinic_id", 22)) .add(Restrictions.Eq("dr.city", "abc_city")) .list(); // go through the patients and set the properties something like this: for(Patient p : patients) { p.lastName = "new lastname"; p.firstName = "new firstname"; } 

有些人认为使用CreateCriteria很困难。 它需要一点点习惯,但是它具有类型安全的优点,并且复杂性可以很容易地隐藏在generics类之后。 谷歌的“Hibernate java GetByProperty”,你明白我的意思。

 update Patient set last_name = :new_last , first_name = :new_first where patient.id = some(select doctor_id from Doctor as doctor where clinic_id = 22 and city = abc_city) 

使用select执行更新和实际将记录提取到客户端,更新它们并将其发布回来之间存在显着差异:

 UPDATE x SET a=:a WHERE b in (SELECT ...) 

在数据库中工作,没有数据传输到客户端。

 list=CreateCriteria().add(Restriction).list(); 

将所有要更新的记录带到客户端,更新它们,然后将它们发回数据库,每个记录可能有一个UPDATE。

使用UPDATE比使用标准快得多 (想想成千上万次)。