Reputation: 6802
I am trying to build a subquery for a QueryDSL select that uses an exists clause in a BooleanExpression.
The data model is such that there are projects which contain a single media. Media can have many dimensions. I am looking to select projects that have certain dimensional data and am using a subquery to accomplish that.
The subquery looks like this:
QProject project = QProject.project;
QMedia media = QMedia.media;
Predicate subExpression = JPAExpressions.selectOne()
.from(media)
.innerJoin(media.dimensions)
.where(project.media.id.eq(media.id),
dimension.dimensionType.id.eq(Long.valueOf(inputDimensionType))).exists();
I store this as a predicate but, when I try to utilize it inside of a parent query I get an error: antlr.NoViableAltException: unexpected token: elements
The generated portion that is causing the error looks something like this (from the hibernate.hql.internal logs):
... and ((exists (select 1
from com.app.model.Media media
inner join elements(media.dimensions)
That is the error that happens when I plug it into a master query like this:
JPAQuery<ResponseCurve> query = new JPAQuery<>(this.entityManager);
query.select().from(project)
.where(project.state.eq(inputState))
.where(subExpression);
Upvotes: 2
Views: 2002
Reputation: 3913
There are two possible issues with this query:
dimensions
, but you never associated it with the join to media.dimensions
dimension.dimensionType
without a join. For identifier values this is possible, for any other property, it is not.What about:
QProject project = QProject.project;
QMedia media = QMedia.media;
QDimension dimension = QDimension.dimension;
Predicate subExpression = JPAExpressions.selectOne()
.from(project.media, media)
.innerJoin(media.dimensions, dimension )
.on(dimension.dimensionType.id.eq(Long.valueOf(inputDimensionType))
Upvotes: 3