Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subquery in Select Projections.constructor

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?

like image 746
Yunus Einsteinium Avatar asked Nov 07 '22 18:11

Yunus Einsteinium


1 Answers

I'm not very familiar with QueryDSL but the error is quite specific.

There is no constructor for that takes two float values which, in your case come from:

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

and:

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

The API http://www.querydsl.com/static/querydsl/4.0.5/apidocs/com/querydsl/core/types/Projections.html states that the possible uses of Projections.constructor are:

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

which means that you are not making the call correctly. Read the documentation more carefully and search for examples, basically you're misusing the API.

like image 123
Mário Fernandes Avatar answered Nov 30 '22 09:11

Mário Fernandes