Sandah Aung
Sandah Aung

Reputation: 6188

JPQL query for entities belonging to another entity and satisfying certain conditions

I have a Patient entity which holds a list of Appointment objects. Appointment has no knowledge of Patient. I would like to create a JPQL query which finds appointments that belong to a specific patient between specified dates. It looks as if I need to write this query inside Appointment but since Appointment does not have a field of type Patient, I am finding it difficult to do so.

Edit (with more information):

I know I have to do it using some sort of join but I don't know much about joins to write the query.

Edit (with links to entities):

Appointment

Patient

Upvotes: 0

Views: 260

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

You simply need a join:

select a from Patient p
join p.appointments a
where p.id = :patientId
and a.date between :start and :end

Whe you don't know much about something, the best way to know more about it is to read the documentation.

Upvotes: 1

Related Questions