本文介绍了Hibernate列是指所有细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表订单和一个表 order_items (一个订单有很多 order_items )。
我希望在实体中有一个列,它引用了订单项中列值的总和。有没有办法在Hibernate中做到这一点?在每次更新或插入相关订单项时,我想重新计算订单表中的值。
例如:

订单a:在 order_items 中有3条记录,其值为1,2,3 。订单中的栏目总数必须为6。

这应该可以通过注解和一个子选项,进行添加。



请注意,只有在实体从数据库重新加载时才会更新该值。



如果您希望在更改期间更新对于你的java代码中的实体,你必须在你的实体中用普通的旧java代码实现它。


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.

解决方案

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.

这篇关于Hibernate列是指所有细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:50