Hibernate hql - help querying foreign key

喜你入骨 提交于 2019-12-06 04:48:33

问题


I am trying to query a foreign key patientId from table appointments.

my Appointment object is mapped to my Patient object (don't know if it matters for the hql) like so:

    <many-to-one name="patient" class="application.model.Patient" fetch="select">
        <column name="patientId" not-null="true" />
    </many-to-one>

and my query is:

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

I have tried to do joins like:

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

I must be missing something fundamental because "appt.appointmentId = 1" works just fine. Any suggestions will be appreciated.


回答1:


HQL is a object query language and since you have a refernce you need to access the reference first to get the id. Assuming the patient class has a property patientid

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


来源:https://stackoverflow.com/questions/9621856/hibernate-hql-help-querying-foreign-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!