ses
ses

Reputation: 13342

jpql query manytomany

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

Answers (2)

Elvander
Elvander

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

Pere Villega
Pere Villega

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

Related Questions