QueryDSL Unexpected token, with any () and in clause
I have two models: Location
and LocationAttribute
, with a Many to Many relationship. I have a list of LocationAttribute IDs and would like to find all locations that have at least one of these attributes.
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;
}
I tried the following QueryDSL code:
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);
The code works fine if locationAttributeIds has 0 or 1 element. However, when I have more than one item, I get this error:
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)
I have found quite a few related sites with my question, but not sure how I would fix the problem.
- https://github.com/querydsl/querydsl/issues/72
- How to set collection items to in-clause in jpql?
- https://hibernate.atlassian.net/browse/HHH-6913
- https://github.com/querydsl/querydsl/issues/271
I made a quick test project, you can find it on Github: https://github.com/bekoeppel/QueryDslInErrorTest . mvn test
throws the above error.
I would appreciate your ideas how can I find everyone Locations
that has at least one from LocationAttribute.id
the list. Thank!
source to share
Use the following JPAQuery constructor instead
new JPAQuery(entityManager);
JPQLTemplates
provides generic serialization that doesn't find all variations of Hibernate JPQL. With only the EntityManager argument, Querydsl will select the correct instance of the JPQLTemplates subclass for you.
source to share