I came to a problem where I need to put ordering by multiplication of two columns of entity, for the sake of imagination entity is:
@Entity
public class Entity {
@Column(name="amount")
private BigDecimal amount;
@Column(name="unitPprice")
private BigDecimal unitPrice;
.
.
.
many more columns
}
My repo interface implements JpaRepository and QuerydslPredicateExecutor, but I am struggling to find a way to order my data by "amount*unitPrice", as I can't find a way to put it into
PageRequest (new Sort.Order(ASC, "amount * unitPrice")
)
without having PropertyReferenceException: No property amount * unitPrice... thrown.
I can't user named query, as my query takes quite massive filter based on user inputs (can't put where clause into query, because if user hasn't selected any value, where clause can't just be in query).
To make it simple. I need something like findAll(Predicate, Pageable), but I need to force that query to order itself by "amount * unitPrice", but also have my Preditate (filter) and Pageable (offset, limit, other sortings) untouched.
Spring
Sort
can be used only for sorting by properties, not by expressions.But you can create a unique sort in a
Predicate
, so you can add this sort-predicate to your other one before you call the findAll method.