Our Seam project has some polymorphic tables
Code:
@Entity
@Table(name = "superclass")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Superclass { ....
We do polymorphic queries:
Code:
entityManager.createQuery("SELECT o from Superclass o WHERE ....")
Sometimes we need to introspect to determine which subclass was actually retreived. We avoid directly checking against the concrete subclasses, and always check against interfaces:
if (obj instanceof ISomeInterface) { .... }
But we are still seeing cases where instanceof returns null, and ClassCastExceptions when we cast a proxied to one of these interfaces.
I am seeking official guidlines for when it is safe to check instanceof (what ancestral classes or interfaces can be checked?) and when it is safe to cast. I'm beginning to think the answer is "never", however even the official Hibernate docs recommend using instanceof inside entity equals() functions, so it must be safe _sometimes_...
Using JPA FETCH JOIN is impractical in our application -- is there a portable way to ensure that an object that might be a proxy is safe to cast (or perhaps explicitly re-FETCH it from the entityManager? (I've tried entityManager.find(Subclass.class, pk), but I keep getting handed back the original (cached?) proxy.
(I know about HibernateProxy.writeResult(), but we're trying to keep this application portable to other JPA implementations...)
Thanks for your help!
Steve
Hibernate version: 3.2.4 sp1
Hibernate Entitymanager version: 3.3.1.ga
Hibernate Annotations version: 3.3.0.ga
Hibernate Commons Annotations version: 3.3.0.ga