Hibernate version: 3.0.5
Mapping documents: N/A
I'm having some serious issues with understanding the boundaries surrounding using proxies in conjunction with using reflection.
In the normal case, when manipulating a managed instance and calling
methods, the CGLIB library interception code successfuly manages access
to object methods so something like:
Code:
Cat c = getCat(); // load persistent cat
Cat mate = getCatMate(); // another persistent load
c.setMate(mate);
... and in Cat.java
public void setMate(Cat c)
{
this.mate = c;
}
means that within setMate, the access to the mate field is referring to the
actual mate field within Cat, not the synthetic field within the proxy if c
is a proxy at the time of call.
However, when using reflection to perform the same invocation:
Code:
...
Method m = getMethod("setMate") ; // get the method object
....
Cat p = getCat(); // here we get a proxy
Cat m = getCat(); // here we get a proxy
m.invoke(p, new Object[] {m});
...
the
this in setMate is the proxy object rather than the implementation
and so, setting this value does not actually change the managed object
and it ends up not having the
mate field set.
What gives? Is this the intended behaviour? How do I make it work in
the presence reflection without unproxying calls all over the place?