Hibernation: is it possible to store the transient field in the second level cache?

I have a requirement to keep track of the field of a persistent object that will change very often and is not really a feature of this object, but rather some kind of caculation. Sometimes the user wants to see this calculation, but this case is very rare (one month or so). So the result of this calculation has to be saved somehow, but I want to avoid saving it to the database every time it changes, since this triggers the update event of the parent object every time (which will create unnecessary database transactions, user notifications and etc.).

The first problem to solve this problem was to make the field temporary, but keep it in the second level cache while sleeping. I haven't found a way to do this by looking at the documentation, but maybe I'm seeing a latent possibility. Do you know if it is possible and how it is possible?

(I have other possibilities for solving this problem, for example using a container data grid, in my case Infinispan JBoss - but this would require a different solution for each application server. I just want to see if there might be an alternative that could be independent of container)


source to share

1 answer

If the calculation is a SQL function, you can use @Formula (instead of in-app calculation and storage).

As for the L2 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 completely ignored.



All Articles