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...