Hibernate column refer the total of details

大憨熊 提交于 2019-12-13 07:46:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!