-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Reflection, interception and proxies
PostPosted: Fri Jun 09, 2006 3:13 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
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?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 7:02 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Reflective method invocation woks without problems with proxies, probably you are using reflection to set fields directly on proxy object.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 7:55 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Baliukas,

baliukas wrote:
Reflective method invocation woks without problems with proxies, probably you are using reflection to set fields directly on proxy object.


I am certainly not setting fields directly - I know the way the proxy works
is by intercepting methods and not by intercepting the getfield operation.
The reflection method I use, as noted in the example above is using
methods declared on the object.
However, I think the problem may still be mine (although I would like to
find out a way around it): the mapping files define these particular fields
as access="field" and so, maybe the usual interception does not
take place? If that is the case I'll need to be change my code.

The question this raises is whether it is advisable to use access=field
in general since then Hibernate will not be influenced by any of the
methods in the class, thus leaving the class implementation free to
implement some validation/additional work without needing to know that
the caller is Hibernate and the instance is being loaded from the DB?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:20 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
anyone?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 10:16 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
bonnyr wrote:
Baliukas,

baliukas wrote:
Reflective method invocation woks without problems with proxies, probably you are using reflection to set fields directly on proxy object.


I am certainly not setting fields directly - I know the way the proxy works
is by intercepting methods and not by intercepting the getfield operation.
The reflection method I use, as noted in the example above is using
methods declared on the object.
However, I think the problem may still be mine (although I would like to
find out a way around it): the mapping files define these particular fields
as access="field" and so, maybe the usual interception does not
take place? If that is the case I'll need to be change my code.

The question this raises is whether it is advisable to use access=field
in general since then Hibernate will not be influenced by any of the
methods in the class, thus leaving the class implementation free to
implement some validation/additional work without needing to know that
the caller is Hibernate and the instance is being loaded from the DB?


Are there common opinions about the question I raised above?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.