Reputation: 13342
software <-m:n-> tag
I want to create query for selecting all softwares where tag.id = id
I write:
TypedQuery query =
Software.em().createQuery(
"SELECT DISTINCT s FROM Software s INNER JOIN s.tags WHERE s.tags.id = :tagId",
Software.class
);
query.setParameter("tagId", tagId);
as result i have:
A java.lang.IllegalArgumentException has been caught, org.hibernate.QueryException: illegal attempt to dereference collection [software0_.id.tags] with element property reference [id] [SELECT DISTINCT s FROM models.Software s INNER JOIN s.tags WHERE s.tags.id = :tagId]
How could I implement it? and why I have such exception?
Upvotes: 1
Views: 918
Reputation: 71
I think the problem might be that you are missing the FROM clause in your statement. The error "Unexpected token: INNER" is given because it expects a FROM.
Try the following query:
SELECT DISTINCT s FROM Software s INNER JOIN s.tags t WHERE t.id = :tagId
Upvotes: 2
Reputation: 16439
I would try with:
Query q = JPA.em().createQuery("SELECT DISTINCT s FROM Software s join fetch s.tags t WHERE t.id = :tagId");
q.setParameter("tagId", tagId);
This should work.
Upvotes: 1