elly (1) [Avatar] Offline
#1
I've got a PatientPlan table with a many-to-one relationship with a Patient table, thus:

<many-to-one >
name="patient"
class="com.zts.hygeia.dao.Patient"
not-null="true"
>
<column name="patient_id" />

As such, a Patient object is contained within the PatientPlan POJO.

To get rows for a given patient with a Criteria query, and desiring to use DetachedCriteria to pass to a method that has access to the Session object, I find I have to code like this:

DetachedCriteria planCriteria = DetachedCriteria.forClass(PatientPlan.class)
.createCriteria("patient")
.add(Restrictions.eq("patientId", patient.getId()));
;
But this leads to an unnecessary join of the two tables:
select... from patient_plans this_ inner join patients patient1_ on this_.patient_id=patient1_.patient_id...

The join only hurts--I'd rather have a query that goes "where this_patient_id='<value>'".

Coding
.add(Restrictions.eq("patientId", patient.getId()))
leads to
org.hibernate.QueryException: could not resolve property: patientId of: PatientPlan.

Similar results ensue from using:
.add(Restrictions.eq("patient.patientId", patient.getId()))

What's the proper way to use an object from a contained POJO class in a Criterion in a Criteria query? Can this join be avoided?

Thanks,
Elly