I have the following minimal example of entities that show my problem:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractProduct {
@Id @GeneratedValue
private Long id;
}
@Entity
public class Product extends AbstractProduct {
//...
}
@Entity
public class ProductOrder {
@Id @GeneratedValue
private Long id;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private AbstractProduct product;
}
The ProductOrder class references an AbstractProduct using @ManyToOne. In this example the concrete implementation will always be Product, bust that's just to keep it simple. If I now load a ProductOrder from the db and cast the AbstractProduct to Product I get a "java.lang.ClassCastException: mypackage.entity.AbstractProduct_$$_javassist_3 cannot be cast to mypackage.entity.Product".
Code:
ProductOrder orderFromDb = (ProductOrder) session.get(ProductOrder.class, order.id);
Product product = (Product) orderFromDb.product;
How would I get access to the actual Product implementation when using lazy loading? Is there anything that I am doing wrong? Or do I have to switch to eager loading? I would have thought that the proxy could deal with such situations. But maybe I am wrong.