包含set的QueryDSL JPA语法错误? [英] QueryDSL JPA syntax error with contains on Set?
问题描述
我有一个类似于以下内容的JPA实体bean:
I have a JPA entity bean similar to the following:
@Entity
class License {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "LicenseTags")
Set<Tag> tags;
// Skipped remaining members
}
Tag
本身也是具有ID和名称的Entity
.现在,我要查询已附加某些标签的许可证.当我尝试以下查询时
Tag
itself is also an Entity
with an id and a name. Now I want to query for licenses that have certain tags attached to them. When I try the following query
Set<Tag> tags = ...;
final QLicense license = QLicense.license;
JPAQuery q = new JPAQuery(entityManager).from(license);
for (Tag tag : tags) {
q.where(license.tags.contains(tag));
}
Collection<License> result = q.listDistinct(license);
我在listDistinct
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select distinct license
from License license
where ?1 in elements(license.tags)]: unexpected token [in].
Internal Exception: NoViableAltException(35!=[685:1: inExpression[boolean not, Object left] returns [Object node] : (t= IN n= inputParameter | t= IN LEFT_ROUND_BRACKET (itemNode= inItem ( COMMA itemNode= inItem )* | subqueryNode= subquery ) RIGHT_ROUND_BRACKET );])
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1328)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:425)
at com.mysema.query.jpa.impl.DefaultSessionHolder.createQuery(DefaultSessionHolder.java:35)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:139)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:108)
at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:276)
at com.mysema.query.support.ProjectableQuery.listDistinct(ProjectableQuery.java:104)
从解析器异常输出中,我只能猜测也许缺少括号.
From the parser exception output I can only guess that maybe there are parenthesis missing.
我在查询集合中包含的值时做错了吗?
Am I doing something wrong to query for values contained in the set?
我正在使用GlassFish Server Open Source Edition 3.0.1(内部版本22),该版本又使用EclipseLink捆绑版本:2.0.1.v20100213-r6600
I am using GlassFish Server Open Source Edition 3.0.1 (build 22) which in turn uses EclipseLink Bundle-Version: 2.0.1.v20100213-r6600
问候,蒂尔曼
推荐答案
好像在JPA查询中使用的是Hibernate模板.试试这个
Looks like you are using the Hibernate templates in your JPA query. Try this instead
JPAQuery query = new JPAQuery (entityManager, EclipseLinkTemplates.DEFAULT);
从下一个版本开始,将自动检测JPA提供程序,并根据此选择适合JPQL使用的模板.
From the next release on there will be an autodetection of the JPA provider and proper templates for JPQL usage will be chosen based on that.
尽管在参考手册您也可以尝试这样表达您的查询
Also you might try to express your query like this
List<License> result = query.from(license)
.where(license.tags.any().in(tags))
.listDistinct(license);
这篇关于包含set的QueryDSL JPA语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!