Hello all
I have an persistent object type FooBar, with properties foo and bar.
foo is stored in the database, but bar is calculated on demand:
Code:
@Transient
public Object getBar() {
if(bar == null) {
bar = expensiveCalculation( getFoo() );
}
return bar;
}
Objects of type FooBar are stored in a second level cache. However, the
'master copy' in the cache has bar == null, so the expensiveCalculation
is happening more often than I would like.
Is there any way to modify @Transient to tell the framework to invoke
the getter after the Object is loaded and its fields populated, but
before it is used (to populate the cache or for anything else)? Ideally
I'd like it to be cache aware i.e. called only if the 2nd level cache is
on. That way copy in the cache would have the calculated value and
(hopefully) so would all the instances cloned from it.
I can't use cache callbacks without coupling the app to a particular
cache implementation and even then my preferred implementation does not
define the behaviour if the 'onPut' callback handler modifies the
object.
I can't use hibernate's onLoad event as it's called before the Object's
fields are populated and the calculation requires them. I thought of
calling getBar from the setFoo method, but that causes problems if foo
is a lazy-loaded collection.
Anyone got any other suggestions?
Thanks.