-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: migrating from 2 to 3: instanceof results change
PostPosted: Fri Jun 03, 2005 7:27 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
I have an abstract class A and a concrete subclass B. These are mapped as follows:

Code:
<class name="A" table="ta">   
  <subclass name="B" discriminator-value="B" proxy="B">


Say that I have an instance of B, sampleB, with id 1. By doing
Code:
   A sampleA  = (A)session.load(cls, new Integer(1));


if I ask
Code:
   sampleA instanceof B

using hibernate 2, I get "true", using hibernate 3, I get "false". Both version use the same classes, server, hibernate configuration, appServer, data.

Tracing the load process, the object sampleA returned is actually in both cases an instance of B proxied by CGLIB,
i.e. an

B$$EnhancerByCGLIB$$75bc@3590"B@6fa"

In the migration guide, if I understood correctly, the only reported change which may affect this is the lazy="false" attribute, but by setting it, the issue above remains.

Is it possible to get consistent answers ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 9:35 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
which one did you lazy="false"?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 9:37 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
For the moment I did it this way, but its ugly:

Code:
   /**
   * beware: persistentObject will be initialized
   */
  public static boolean instanceOfPersistent(Object persistentObject, Class aClass) {

    boolean result = false;
    Class pClass = null;
    if (persistentObject instanceof HibernateProxy) {
      HibernateProxy proxy = (HibernateProxy) persistentObject;
      AbstractLazyInitializer li = (AbstractLazyInitializer) proxy.getHibernateLazyInitializer();

      //this does not work
      //Class bclass = HibernateProxyHelper.getClassWithoutInitializingProxy(persistentObject);
      // hence I use this which is quite ugly, as I fear the object is uselessly initialized:     
      pClass = li.getImplementation().getClass();
    } else
      pClass = persistentObject.getClass();

    if (ReflectionUtilities.getInheritedClasses(pClass).contains(aClass))
      result = true;
    else
      result = false;
    return result;
  }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 9:46 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
Answer to Emmanuel:

first I put lazy="false" on B, no improvements;

then I put lazy="false" on both A and B, no improvements;

then I put lazy="false" only on A, and.. it works!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 9:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this does not answer my question

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 10:16 am 
Beginner
Beginner

Joined: Fri Aug 29, 2003 10:01 am
Posts: 34
Location: florence, italy
So the final question is: is there a way to be sure that instanceof gives me the correct answer, keeping my objects lazy ? in previous versions of my horrible hack above, I tried to access the "target" field of the proxy, and asking its class, instead of forcing initialization, but even using reflection and extension tricks it has been carefully protected, via privateness and final classes.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 6:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate.getClass()
but is will initialize the proxy by side effect. This is not what you want.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.