Hibernate hql - 帮助查询外键

时间:2012-03-08 17:14:01

标签: hibernate hql

我正在尝试从表约会中查询外键患者。

我的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();

我必须遗漏一些基本因素,因为&#34; appt.appointmentId = 1&#34;工作得很好。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:16)

HQL是一种对象查询语言,因为你有一个引用,你需要先访问引用来获取id。假设患者类别具有属性

createQuery("from Appointment as appt where appt.patient.patientId = 1").list();