-->
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.  [ 6 posts ] 
Author Message
 Post subject: CGLIB Proxy breaks identity with 'this' pointer?
PostPosted: Mon Aug 01, 2005 8:10 am 
Newbie

Joined: Mon Aug 01, 2005 6:47 am
Posts: 3
Location: Belgium
Hi there,

I'm using hibernate 3.0.5 (cglib 2.1), and I implemented a Visitor pattern like this (see also http://www.hibernate.org/280.html):

Code:
interface Visitor
{
  void visit(Facility f);
  ...
}
class Node //hibernate persisted class

}
class Facility extends Node  //joined-subclass
{
   public void accept(Visitor visitor)
   {
     visitor.visit(this);
   }
   ...
}
class ConcreteVisitor extends Visitor
{
   public void visit(Facility facility)
   {
      this.visited = facility;
   }
}

Now the folowing code fails:

Code:
Visitor vtor = new ConcreteVisitor();
Facility proxy = session.load(Facility.class, id);
proxy.accept(vtor);
Facility visited = vtor.getVisited();
assertEquals(proxy.getName(), visited.getName());//OK
assertEquals(proxy.getId(), visited.getId());//OK
//assertSame(proxy, visited); //FAIL
assertEquals(proxy, visited); //FAIL

Debugging showed that the proxy is of Class Facility$$Enhanced..., as I expected, but the visited is of type Facility, not enhanced! The content is is the same, e.g. visited.getDepartments() is a PersistentSet, but they are not the same object. I did not implement equals or hashCode, so assertEquals fails too.

It is guaranteed that in Hibernate, if the same entity is loaded more than once in the same session, they get the same proxy object. Shouldn't the 'this' pointer also point to the proxy class?

Is this related to the fact that I'm using a joined-subclass? I suppose not, since I explicitly load an instance of the subclass...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 10:31 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Interceptor implementation transforms parameters for some reason, probably it has a good reason, but I do not remember it. You can customize this default implementation using hibernate persister and proxy factory API.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 10:37 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Sorry, I remember it. This stuff helps to avoid stack overflow in some cases, so it must be just not to depend on "proxy == visited" asumption than to customize lazy loading.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 12:38 pm 
Newbie

Joined: Mon Aug 01, 2005 6:47 am
Posts: 3
Location: Belgium
Do you mean that inside a utility method in a proxied class, the 'this' pointer does not point to the proxy itself, but to some 'transformed' instance? Does this transformed instance have the same functionality/interceptors as the proxy class?

More concrete, if I do visited.setName(newName), will this be persisted, as when I would do proxy.setName(newName)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 12:50 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
greyfairer wrote:
Do you mean that inside a utility method in a proxied class, the 'this' pointer does not point to the proxy itself, but to some 'transformed' instance? Does this transformed instance have the same functionality/interceptors as the proxy class?

More concrete, if I do visited.setName(newName), will this be persisted, as when I would do proxy.setName(newName)?


Interceptor replaces "this" in method parameter array before to delegate, all calls are delegated to "real" instance and proxy has no persistent state itself, so it will be no problems with "visited.setName(newName)".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 02, 2005 7:44 am 
Newbie

Joined: Mon Aug 01, 2005 6:47 am
Posts: 3
Location: Belgium
Thank you for you answer. I will make sure not to depend on object equality, but instead on proxy.getId().equals(visited.getId()).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.