2012-03-08 63 views
3

我想从表约会中查询外键的patientId。Hibernate hql - 帮助查询外键

我的约会对象映射到我的病人对象(不知道这是否事项的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”的作品就好了。任何建议将不胜感激。

回答

14

HQL是一种对象查询语言,由于您有一个引用,您需要先访问引用以获取该id。假设患者类别具有房产特性

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