Hibernate HQL——查询外键帮助

4

我希望查询约会表中的外键患者ID。

我的约会对象映射到患者对象上(不知道对于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"可以正常工作。如有建议,将不胜感激。

1个回答

17

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接