I tried looking up some other user forums on this and I dont see the solution to my problem. I read about de-proxy and visitor pattern solutions but thats not what I want.
Problem:
I'm trying to use a hibernate object to return its subclass using a given method.
Ex. A extends B extends AbstactAB
Code:
class abstract AbstractAB{
public A getA()
{
return (A)this; // this way throws class cast in calling code
return new A(); // this way works fine but obviously not what i want
}
//calling code
A obj = b.getA(); //ClassCastException
for the record i'm actually using the visitor pattern below to get subclass,
after some digging the visitor way is doing this the same thing above, so i suspect its not a visitor problem its something to do with hibernate implementation of proxying an instance.
Code:
class AbstractAB{
private Visitor v = new SubClassVisitor()
public abstract accept(Visitor v);
public A getA()
{
accept(v);
return v.getA();
}
}
public class A
{
public void accept(Visitor v)
{
v.visit(this);
}
}
Public class SubClassVisitor implements Visitor
{
private A a;
public void visit(A a)
{
this.a = a;
}
public A getA()
{
return a;
}
}
I dont want to turn lazy loading off and I really dont want to use some deproxy method, is there some way around this? I had a solition where created some static class that returns A off the proxy object B but i dont what to use it that way. It would be nice to have the get subclass method on the object itself.