Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: JPA: instanceof vs. hibernate proxy: when is it safe?
PostPosted: Thu Jun 12, 2008 5:48 pm 
Newbie

Joined: Fri Sep 21, 2007 9:58 pm
Posts: 6
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 9:19 am 
Regular
Regular

Joined: Thu Dec 22, 2005 7:47 am
Posts: 62
Location: Czech Republic
There was a very helpfull thread and the receipt it provides can be summarized to the following method:

thread: http://forum.hibernate.org/viewtopic.php?t=962876&highlight=enhancerbycglib+classcastexception

thanks goes to frusso, http://forum.hibernate.org/profile.php?mode=viewprofile&u=15242

Code:
   /**
    * Utility method that tries to properly initialize the Hibernate CGLIB
    * proxy.
    *
    * @param <T>
    * @param maybeProxy -- the possible Hibernate generated proxy
    * @param baseClass -- the resulting class to be cast to.
    * @return the object of a class <T>
    * @throws ClassCastException
    */
   public static <T> T deproxy(Object maybeProxy, Class<T> baseClass) throws ClassCastException {
      if (maybeProxy instanceof HibernateProxy) {
         return baseClass.cast(((HibernateProxy) maybeProxy).getHibernateLazyInitializer().getImplementation());
      }
      return baseClass.cast(maybeProxy);
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 9:37 am 
Newbie

Joined: Fri Sep 21, 2007 9:58 pm
Posts: 6
Thanks -- that's much like the deproxy() function we're currently using as a workaround, but I _really_ had hoped that there was a cleaner solution.

FWIW, we're using the following code which is different than the previous poster's, but also seems to work:
Code:
if (object instanceof HibernateProxy) {
   HibernateProxy proxy = (HibernateProxy)object;
   object = proxy.writeReplace();
}


The problem, of course, is that this is very fragile -- it pushes a dependency on the nature of the underlying persistence implementation into my business logic. If one tries to cast or instanceof without first calling deproxy, one gets either incorrect behavior (instanceof returns false), or an exception (ClassCastException) -- not to mention that it ties my application to Hibernate instead of being a clean portable EJB3 application.

My reading of the EJB3/JPA specs suggest that this should not be necessary. Is this acknowledged as a Hibernate JPA bug?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.