Hibernate hql - help querying foreign key

2019-04-11 08:12发布

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.

标签: hibernate hql
1条回答
地球回转人心会变
2楼-- · 2019-04-11 08:34

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();
查看更多
登录 后发表回答