简体   繁体   中英

QueryDSL selecting values with alias from Sub Query

I have two entities as below

Parent {
String parentID;
String parentName;
...
}

-

Child{
String parentID;
String childID;
Integer height;
....
}

Its a one to many relationship between the parent and the child.

My intention is have the below SQL.

    SELECT p.parentID, p.parentName, c.childrenHeightRange
    FROM
    Parent p
    INNER JOIN
    (
    SELECT parentID, (CASE WHEN (min(height) = max(height)) THEN cast(min(height) as varchar) ELSE cast(min(height) as varchar) + ' - '  + cast(max(height) as varchar) END) as childrenHeightRange
FROM Child GROUP BY parentID) as c
    ON (p.parentID = c.parentID)

For above query, I used a CaseBuilder for childHeightRange as below

   JPASubQuery subQuery = new JPASubQuery().from(child).groupBy(child.parentID);

    Expression<String> heightCaseExpression = new CaseBuilder()
            .when(child.height.min().eq(child.height.max()))
            .then(child.height.min().stringValue())
            .otherwise(child.height.min().stringValue() +"-"+child.height.max().stringValue())
            .as("childrenHeightRange");

And then comes the big question..

I have

query.from(parent).innerjoin(subquery.list(child.parentID,heightCaseExpression )).on(parent.parentID.eq(child.parentID))
 .list(parent.parentID,parent.parentName, ............);

How do i reference childrenHeightRange from the subquery?

Please help.

Thanks

在 Querydsl JPA 中,您只能在 where 部分使用子查询,因此您需要重新构建查询。

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