I am following the A to B relationship (many to one) where B is abstract and the relationship lazy, I get a ClassCastException:
java.lang.ClassCastException: com.mycompany.myproject.MyEntityB_$$_javassist_1
at com.mycompany.myproject.MyEntityB_$$_javassist_1.getMyEntityB1(EntiteB_$$_javassist_1.java)
at com.mycompany.myproject.MyServiceImpl.getMyEntityB1FromA(MyServiceImpl.java:68)
java.lang.ClassCastException: com.mycompany.myproject.MyEntityB$$EnhancerByCGLIB$$22ea3be1
at com.mycompany.myproject.MyEntityB$$EnhancerByCGLIB$$22ea3be1.getMyEntityB1(<generated>)
at com.mycompany.myproject.MyServiceImpl.getMyEntityB1FromA(MyServiceImpl.java:68)
@Entity public class MyEntityA { @Id private int id; private String name; @ManyToOne(fetch=FetchType.LAZY) private MyEntityB myEntityB; ... } @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class MyEntityB { @Id private int id; private String name; @OneToMany(mappedBy="MyEntityB") private List<MyEntityA> myEntitiesA=new ArrayList<MyEntityA>(); ... } @Entity public class MyEntityB1 extends MyEntityB { @OneToMany(mappedBy="myEntityB1") private List<MyEntityB2> myEntitiesB2=new ArrayList<MyEntityB2>(); ... } @Entity public class MyEntityB2 extends MyEntityB { @ManyToOne private MyEntityB1 myEntityB1; ... }
I found similar problem in Hibernate forum, but still no clean solution:
http://forum.hibernate.org/viewtopic.php?p=2371113