Hello,
we have a problem with inheritance and TypeCast.
Let me show you the situation first:
Code:
@Entity
public class AClassImpl implements AClass{
@ManyToOne(fetch=FetchType.LAZY, targetEntity=BClassImpl.class)
private BClass bClass;
/* getter setter here*/
}
Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class BClassImpl implements BClass{
@OneToMany(targetEntity = AClassImpl.class, mappedBy = "bClass")
private List<AClass> aClasses;
/* getter setter here*/
}
Code:
@Entity
public class CClassImpl implements CClass extends BClass{
}
The Interfaces have the same inheritance than the actual classes have.
CCLass extends BClass
I am in the situation, that AClass can have either BClass or CClass. It works without a problem.
EXCEPT the one most important thing! If AClass references a instance of CClass and if I do a session.get on AClass and later try to access the property bClass (which is actaully a cClass now) I get a TypeCastException when I try to do this:
Code:
(CClass) myAClass.getBClass();
Because bClass is a Hibernate Proxy instead of the real BClass type. (Which I coulld cast to CClass without a prolbem.)
The sollution to this is to change the FetchType.LAZY to FetchType.EAGER. But I don't want the eager fetching in this situaion. Is there a sollution for this problem?
any help is greatly apreciated.
Santo.