31. What are derived properties

The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.


These properties are by definition read-only. You declare the computation as an SQL expression. This then translates to a SELECT clause subquery in the SQL query that loads an instance:

<property name="totalPrice"

formula="( SELECT SUM (li.quantity*p.price) FROM LineItem li, Product p
WHERE li.productId = p.productId
AND li.customerId = customerId
AND li.orderNumber = orderNumber )"/
>
You can reference the entity table by not declaring an alias on a particular column.
This would be customerId in the given example. You can also use the nested <formula> mapping element if you do not want to use the attribute.




No comments:

Post a Comment