Jay Witherspoon
Jay Witherspoon

Reputation: 175

JPA query using named parameter from a oneToOne joined table

I am trying to create a query that references fields in the WHERE from both the current table and a table joined on a column. I have no problems creating a query from a single parameter in the Participation table.

@NamedQuery(name="Participation.byUserID", query="SELECT c FROM Participation c WHERE c.userID = :userID")

Here is the fun part... Within the Participation entity, I have a join:

@OneToOne(optional=false)
@JoinColumn(name = "EventID", insertable = false, updatable = false)
private Event event;

The Event entity has some fields I would like to use in my query. For example, eventDate or eventType.

I am wanting something like WHERE Participation.userID = 123 AND Event.eventType = "meeting" using JPA queries instead of SQL. How can I expand the simple named query above to include comparisons from the joined table? So far I have not been able to get this working, so any help would be appreciated.

Upvotes: 1

Views: 722

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24423

This is a really simple requirement, I suggest you go through some basic JPA tutorials.

SELECT c FROM Participation c WHERE c.userID = :userID and c.event.eventType = :eventType

Upvotes: 2

Related Questions