Subquery in Select Projections.constructor

时间:2017-08-04 12:27:40

标签: mysql hibernate subquery querydsl

Tried to write subqueries in Select clause with Projections like so

queryFactory.query()
            .select(
                    Projections.constructor(
                            MemberPaymentDTO.class,
                            JPAExpressions
                                    .select(coopMember)
                                    .from(coopMember)
                                    .where(memberPayment.memberId.eq(coopMember))
                                    .fetchOne(),
                            JPAExpressions
                                    .select(paymentTransaction.amount)
                                    .from(paymentTransaction)
                                    .where(paymentTransaction.transactionId.eq(memberPayment.paymentTransaction.transactionId))
                                    .fetchOne().floatValue(),
                            JPAExpressions
                                    .select(collectionTransaction.price.multiply(collectionTransaction.quantity).sum())
                                    .from(collectionTransaction)
                                    .where(collectionTransaction.member.memberId.eq(memberPayment.memberId.memberId))
                                    .where(collectionTransaction.paymentPeriod.paymentPeriodId.eq(paymentPeriodId))
                                    .fetchOne().floatValue()

            )
            .from(memberPayment);   

The DTO is as follows

public class MemberPaymentDTO {
    private CoopMember coopMember;
    private float payableAmount;
    private float collectionsAmount;


    public MemberPaymentDTO(CoopMember coopMember, float payableAmount, float collectionsAmount) {
        this.coopMember = coopMember;
        this.payableAmount = payableAmount;
        this.collectionsAmount = collectionsAmount;
    }
}

The problem with the above code is Intellij Compiler complains Cannot resolve method 'constructor(java.lang.Class<re.iprocu.model.MemberPaymentDTO>, re.iprocu.model.CoopMember, float, float)

Is it possible for one to add a subquery to select clause and set in a DTO? How?

1 个答案:

答案 0 :(得分:1)

我对QueryDSL不是很熟悉,但错误非常具体。

没有构造函数需要两个浮点值,在您的情况下来自:

JPAExpressions
                                    .select(paymentTransaction.amount)
                                    .from(paymentTransaction)
                                    .where(paymentTransaction.transactionId.eq(memberPayment.paymentTransaction.transactionId))
                                    .fetchOne().floatValue()

JPAExpressions
                                    .select(paymentTransaction.amount)
                                    .from(paymentTransaction)
                                    .where(paymentTransaction.transactionId.eq(memberPayment.paymentTransaction.transactionId))
                                    .fetchOne().floatValue()

API http://www.querydsl.com/static/querydsl/4.0.5/apidocs/com/querydsl/core/types/Projections.html声明Projections.constructor的可能用途是:

constructor(Class<? extends T> type, Expression<?>... exprs)
Create a constructor invocation projection for the given type and expressions

constructor(Class<? extends T> type, Class<?>[] paramTypes, com.google.common.collect.ImmutableList<Expression<?>> exprs)
Create a constructor invocation projection for given type, parameter types and expressions

constructor(Class<? extends T> type, Class<?>[] paramTypes, Expression<?>... exprs)
Create a constructor invocation projection for given type, parameter types and expressions

表示您没有正确拨打电话。更仔细地阅读文档并搜索示例,基本上您是在滥用API。