I wrote similar code, but found weird behaviour when running JUnit tests
on it. I posted a question on the user forum, http://forum.hibernate.
org/viewtopic.php?t=945748.
Basically,backporting to your code:
Democrat proxy = session.load(Democrat.class, id);
Democrat visited = new PoliticianViewHelper(proxy).getDemocrat();
assertNotNull(visited); //ok
assertEquals(id, visited.id); //ok
assertSame(proxy, visited; //FAILS
It turns out that in the invocation of the accept() method, the 'this'
pointer does not point to the proxy instance, but to a real instance of
the Democrat class.
The Visitor pattern as you described is still very valuable, off course,
but this one surprised me. |