开发者

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 DIST开发者_开发技巧INCT 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜