问题
I have a table order
and a table order_items
(one order
has many order_items
).
I want to have a column in order entity that refers to the sum of the column values in order items. is there a way to do it in Hibernate? At each update or insert of related order items, I want to recalculate the value in order table.
For example:
Order a: has 3 records in order_items
with value 1, 2, 3. The column total on order must be 6.
回答1:
This should be possible with the @Formula annotation and a subselect doing the adding.
Note that the value will only get updated when the entity gets reloaded from the database.
If you want it to update during changes to the entity in your java code you have to implement that in your entities with plain old java code.
来源:https://stackoverflow.com/questions/44051748/hibernate-column-refer-the-total-of-details