2012-03-08 2 views
3

테이블 약속에서 foreign 키 patientId를 쿼리하려고합니다.Hibernate 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(); 

나는 시도과 같이

내 약속 개체가 내 환자 개체에 매핑되는

(는 HQL에 대한 중요한 알고하지 않습니다) 같은 조인을 :

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();