maircink
maircink

Reputation: 91

JPQL Many to Many conditional query

I have two entities: News and Tag with Many to Many relation. News has a Calendar field. I have an array of longs with some Tag ids and a Calendar. I want to get all Newses which have at least one Tag from specified in ids array and only those which have newer Calendar then the one I possess. What is the best way to achive it? Thanks in advance.

Upvotes: 1

Views: 4102

Answers (3)

maircink
maircink

Reputation: 91

Combined answers of JB Nizet and Mikko Maunu:

SELECT DISTINCT n FROM News n INNER JOIN n.tags t
WHERE t.id IN (:tagIds) AND n.timestamp > :timestamp

It requires List to be passed, array won't do.

Upvotes: 2

Mikko Maunu
Mikko Maunu

Reputation: 42114

What you do not show, is type of tagIds. According message in expection, its is array. Because you are comparing against Long, type of parameter should be one of the following: long, Long or List<Long>.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 692121

select n from News n inner join n.tags tag
where tag.id in (:tagIds)
and n.theCalendarField > :calendarParam

Upvotes: 4

Related Questions