我正在尝试从表约会中查询外键患者 ID。
我的 Appointment 对象映射到我的 Patient 对象(不知道它对 hql 是否重要),如下所示:
<many-to-one name="patient" class="application.model.Patient" fetch="select">
<column name="patientId" not-null="true" />
</many-to-one>
我的查询是:
createQuery("from Appointment as appt where appt.patientId = 1").list();
我试图做这样的连接:
createQuery("from Appointment as appt join appt.patientId ptid where ptid.patientId = 1").list();
我一定遗漏了一些基本的东西,因为“appt.appointmentId = 1”工作得很好。任何建议将不胜感激。
最佳答案
HQL 是一种对象查询语言,因为你有一个引用,所以你需要先访问引用来获取 id。假设患者类有一个属性patientid
createQuery("from Appointment as appt where appt.patient.patientId = 1").list();
关于Hibernate hql - 帮助查询外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9621856/