I am trying to create a query where I need to add rounding to one selected field (setting decimal place 1).
This is my code:
JPAQuery query = new JPAQuery(em);
QMyEntity myEntity = QMyEntity.myEntity;
QSecondaryEntity secondaryEntity = QSecondaryEntity.secondaryEntity;
Expression<String> idExpression= (new CaseBuilder()
.when(myEntity.type.eq(4)).then(myEntity.normalId)
.otherwise(myEntity.specialId)).as("id");
NumberExpression<BigDecimal> roundExpression = MathExpressions.round(myEntity.qty, 1).as("roundedQty");
query = query.from(browse, lips).where(predicate);
return query.list(Projections.constructor(MyEntityDto.class, myEntity.field1, myEntity.field2, roundExpression, idExpression));
MyEntity is not directly mapping a database table called "MyEntity", but "Table0".
When I see the generated query I see:
select myEntity.field1, myEntity.field2, round(myEntity.qty),
(case when (myEntity.type = ?1) then myEntity.normalId
else myEntity.specialId end) as id
from MyEntity myEntity
where myEntity.id = ?2 and ...
And I get this error:
The SELECT clause has 'round' and '(myEntity.qty)' that are not separated by a comma.
Whereas if I remove the round expression the generated query is ok and querying the correct table in the database:
SELECT t1.Field1 AS a1, t0.Field2 AS a2, t0.Quantity AS a3,
CASE WHEN (t0.Type = ?) THEN t3.ID ELSE t3.SecondID END
FROM Table7 t7, Table t6,..., Table1 t1, Table0 t0
WHERE ((((((((t ...
Am I using the MathExpression correctly? What is the correct way of rounding a field in the select clause?