休眠:是否可以将暂存字段保存在二级缓存中? [英] Hibernate: Is it possible to save a transient field in second level cache?

查看:139
本文介绍了休眠:是否可以将暂存字段保存在二级缓存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求跟踪持久实体的字段,这个字段会经常改变,并不是该实体的特征,而是某种计算。有时,用户希望看到这种计算,但这种情况非常罕见(一个月左右)。
所以这个计算的结果必须以某种方式保存,但是我想避免每次更改时都将其保存在数据库中,因为每次都会触发父实体的更新事件(这会造成不必要的数据库事务,用户通知等)。

解决这个问题的第一个想法是将字段设置为暂态,但将其保存在休眠的二级缓存中。在查看文档时,我没有找到这样做的方法,但也许我会监督隐藏的可能性。
所以你知道这是可能的吗?



(我有其他的可能性来解决这个问题,例如使用容器的数据网格,我的情况是JBoss的Infinispan - 但这需要为每个应用程序服务器提供不同的解决方案。我只是想确定上述解决方案是否可以独立于容器)

解决方案

如果计算是SQL函数,您可以使用@Formula(而不是在应用程序中计算并存储)。
$ b

关于二级缓存,不会暂存字段。缓存(以及一般的ORM)基于持久对象/属性及其元数据。根据定义,瞬变将被完全忽略。


I have the requirement to keep track of a field of a persistent entity, which will be changed very often and is not really a feature of that entity, rather some kind of caculation. Sometimes, a user wants to see that calculation, but this case is very rare (once month or so). So the result of this calculation has to be saved somehow, but i want to avoid to save it in the database every time it changes, because this would trigger an update event of the parent entity each time (which would create unnecessary database transactions, user notifications etc).

A first idea to solve this issue was to make the field transient, but to save it in the second level cache of hibernate. I didn't find a way to do so when looking in the documentation, but maybe i oversee a hidden possibility. So do you know whether and how this is possible?

(I have other possibilities to solve this issue, e.g. using the data grid of the container, in my case Infinispan of JBoss - but that would require a different solution for every application server. I just want to make sure whether the above-mentioned solution, which would be independent from the container, could be an alternative)

解决方案

If the calculation is an SQL function, you could use a @Formula (instead of calculating in the application and storing).

Regarding the 2nd level cache, no, transient fields are not cached. Caching (and ORM in general) is based on persistent objects/properties and their metadata. By definition, transients are ignored completely.

这篇关于休眠:是否可以将暂存字段保存在二级缓存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆