Projection records marked with this annotation type can specify a SQL query that represents the projection.
Usage examples:
Define a projection record with a SQL query.
Java:
@ProjectionQuery("""
SELECT b.id, COUNT(*) AS item_count, SUM(price) AS total_price
FROM basket b
LEFT JOIN basket_item bi ON b.id = bi.basket_id
GROUP BY b.id""")
record BasketSummary(@PK @FK("id") Basket basket,
int itemCount,
BigDecimal totalPrice
) implements Projection<Integer> {}
Kotlin:
@ProjectionQuery("""
SELECT b.id, COUNT(*) AS item_count, SUM(price) AS total_price
FROM basket b
LEFT JOIN basket_item bi ON b.id = bi.basket_id
GROUP BY b.id""")
data class BasketSummary(@PK @FK("id") val basket: Basket,
val itemCount: Int,
val totalPrice: BigDecimal
) : Projection<Int>
Then, you can use the projection in a query like this:
var baskets = ...
List<BasketSummary> summaries = ORM(dataSource).projection(BasketSummary.class)
.select()
.where(baskets) // Type-safe.
.getResultList();
Or use it as a foreign key in an entity.
Java:
record User(@PK int id,
@FK("basket_id") BasketSummary basketSummary
) implements Entity<Integer> {}
Kotlin:
data class User(@PK val id: Int,
@FK("basket_id") val basketSummary: BasketSummary
) : Entity<Int>
Then, you can query all users having a basket with at least 1 item:
List<User> users = ORM(dataSource).projection(User.class)
.select()
.where(User_.basketSummary.itemCount, GREATER_THAN, 0) // Type-safe metamodel.
.getResultList();
-
Required Element Summary
Required Elements
-
Element Details
-
value
String valueThe SQL query that represents the projection.
-