QueryDSL Unexpected Token ,带有 any() 和 in 子句

QueryDSL Unexpected Token , with any() and in clause

我有两个模型,LocationLocationAttribute,具有多对多关系。 我有一个 LocationAttribute ID 列表,我想找到至少有一个的所有 Locations 这些属性。

Location.java:

@Entity
public class Location {

    @Id
    @GeneratedValue
    @Column(name="LOCATION_ID")
    protected int id;

    @ManyToMany(targetEntity = LocationAttribute.class)
    @JoinTable(name="LOCATION_TO_LOCATION_ATTRIBUTE",
            joinColumns = @JoinColumn(name="LOCATION_ID", referencedColumnName = "LOCATION_ID"),
            inverseJoinColumns = @JoinColumn(name="LOCATION_ATTRIBUTE_ID", referencedColumnName = "LOCATION_ATTRIBUTE_ID")
    )
    private List<LocationAttribute> locationAttributes;
}

LocationAttribute.java:

@Entity
public class LocationAttribute {

    @Id
    @GeneratedValue
    @Column(name="LOCATION_ATTRIBUTE_ID")
    protected int id;
}

我尝试了以下 QueryDSL 代码:

List<Integer> locationAttributeIds = new ArrayList<Integer>();
locationAttributeIds.add(1);
locationAttributeIds.add(2);
locationAttributeIds.add(3);

QLocation location = QLocation.location;
JPAQuery query = new JPAQuery(entityManager, JPQLTemplates.DEFAULT);
query.from(location) .where(location.locationAttributes.any().id.in(locationAttributeIds));
query.list(location);

如果 locationAttributeIds 有 0 个或 1 个元素,代码工作正常。但是,当我有超过 1 个元素时,我会收到此错误:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: , near line 5, column 53 [select location
from ch.locatee.test.querydslerror.locatee.Location location
where exists (select location_locationAttributes_cc6d8
from location.locationAttributes as location_locationAttributes_cc6d8
where location_locationAttributes_cc6d8.id in :x1_0_, :x1_1_, :x1_2_)]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.getResultList(AbstractJPAQuery.java:194)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:246)
    at ch.locatee.test.querydslerror.locatee.AppTest.testSomething(AppTest.java:63)

我发现了很多与我的问题相关的网站,但我不确定如何解决这个问题。

我做了一个快速测试项目,你可以在Github上找到它:https://github.com/bekoeppel/QueryDslInErrorTestmvn test 产生上述错误。

如果您提出如何从列表中找到至少具有 LocationAttribute.id 之一的所有 Locations,我将不胜感激。谢谢!

改为使用以下 JPAQuery 构造函数

new JPAQuery(entityManager);

JPQLTemplates 提供通用序列化,它不会找到 Hibernate 的所有 JPQL 变体。只有 EntityManager 参数 Querydsl 将为您选择正确的 JPQLTemplates 子类实例。