开发者

jpql query manytomany

开发者 https://www.devze.com 2023-02-22 23:11 出处:网络
software <-m:n-> tag I want to create query for selecting all softwares where tag.id = id I write: TypedQuery query =

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

精彩评论

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