简体   繁体   中英

QueryDSL how do I create a SubQuery with its own joins?

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);

There are two possible issues with this query:

  1. You're trying to reference alias dimensions , but you never associated it with the join to media.dimensions
  2. You're dereferencing 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM