Subquery in Select Projections.constructor

Tried to write subqueries in Select clause with such projections

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

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 - Intellij Compiler complains Cannot resolve method 'constructor(java.lang.Class<re.iprocu.model.MemberPaymentDTO>, re.iprocu.model.CoopMember, float, float)

Can I add a subquery to select a proposal and set it in the DTO? How?

+5
source share
1 answer

I am not very familiar with QueryDSL, but the error is quite specific.

There is no constructor for which two float values ​​are taken, 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 use of 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 

which means that you are not making the call correctly. Read the documentation more carefully and look for examples, basically you abuse the API.

+1
source

Source: https://habr.com/ru/post/1270586/


All Articles