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

时间:2015-04-25 13:13:28

标签: java hibernate querydsl

我有两个模型,LocationLocationAttribute,具有多对多关系。 我有一个LocationAttribute ID列表,并希望找到至少有一个的所有位置 那些属性。

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会产生上述错误。

如果我能找到列表中至少有一个Locations的所有LocationAttribute.id,我们将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

使用以下JPAQuery构造函数

new JPAQuery(entityManager);

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