Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate hql - help querying foreign key

Tags:

hibernate

hql

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.

like image 234
slex Avatar asked Dec 03 '22 04:12

slex


1 Answers

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();
like image 86
Firo Avatar answered Feb 02 '23 23:02

Firo