Hi,
Is there a way to map a calculated property using JPA?
Assuming I have an Invoice
object with one or more InvoiceLineItems
within it, I want to have a persistent calculated property on the Invoice
class that gives me the total amount:
class Invoice {
...
@Column(name = "TOTAL_AMOUNT")
public BigDecimal getTotalAmount() {
BigDecimal amount = BigDecimal.ZERO;
for (InvoiceLineItem lineItem : lineItems) {
amount = amount.add(lineItem.getTotalAmount());
}
return amount;
}
}
Now, I could create a protected no-op setTotalAmount
method to make JPA happy, but I was wondering if there is a way to let JPA know that the mapping is one way only and to avoid creating a superfluous setter method.
Thanks, Aleks