How to sort by DTO fields or aliases?
data class BookInfo(
val name: String,
val authorCount: Long
)
// 1. Define an alias for the expression.
val authorCountAlias = expression(Long::class, "authorCount")
val query = jpql {
selectNew<BookInfo>(
path(Book::name),
count(Book::authors).`as`(authorCountAlias) // 2. Use the alias in the select clause.
).from(
entity(Book::class)
).groupBy(
path(Book::name)
).orderBy(
authorCountAlias.asc() // 3. Use the alias in the orderBy clause.
)
}
val bookInfos = entityManager.createQuery(query, context).resultListPreviousHow to handle count query in Spring Data JPA Pageable?NextHow to work with advanced JPA mappings?
Last updated