-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to get instance from proxy that is lazy initialized?
PostPosted: Thu Sep 09, 2004 8:30 pm 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
Below I am trying to get the classMetadata off a lazy object that I initialized (using cglib reflection). Because exp is really just a Proxy, it is returning null. Is there a way to get the underlying instance once you initialized it?

The code is simplified example, but I want to use reflection to some stuff on the object. That's why I need the metadata.

Thanks,
Lou

Code:
    public void testGetExposureType() throws DaoException, HibernateException {
        ClmClaimExposure exp = expDao.find(new Long(948));
        Hibernate.initialize(exp);
        expDao.flushHibernateSession();

        ClassMetadata classMetadata = sesFactory.getClassMetadata(exp.getClass());


        assertNotNull(classMetadata);

    }




Hibernate version:
2.1.6
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="com.mitchell.services.technical.claim.dao.vo">
   <class name="ClmClaimExposure" table="EXPOSURE" lazy="true">
      <id
         name="id"
         type="java.lang.Long"
         column="CLAIM_EXPOSURE_ID"
      >
         <generator class="sequence">
            <param name="sequence">EXPOSURE_ID_SEQ</param>
         </generator>
      </id>

      <version name="tcn" column="TCN" type="java.lang.Long" unsaved-value="null"/>
      <property
         column="CO_CD"
         length="10"
         name="coCd"
         not-null="false"
         type="string"
       />
   </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

NullPointerException

Name and version of the database you are using:

Oracle 9.2.1.0

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 8:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate.getClass()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 9:13 pm 
Regular
Regular

Joined: Thu Jul 01, 2004 12:13 am
Posts: 68
Location: San Diego, CA
Cool...thanks! I was noodling through the javadocs to find an alternative way as follows:

Code:
           
            Class objClass = object.getClass();
            ClassMetadata classMetadata =  sesFactory.getClassMetadata(objClass);
           
            if (classMetadata == null ) {
                // If here, then we are dealing with a CBLIB Proxy class and
                // must use the ProxyHelper to get the real class
                objClass = HibernateProxyHelper.getClass(object);
                classMetadata = sesFactory.getClassMetadata(objClass);
            }


Is there a way to know that you are dealing with a "proxy" class? I could clean this up a bit further knowing that.

Best,
Lou


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 28, 2004 4:29 am 
Newbie

Joined: Thu Jul 08, 2004 7:16 am
Posts: 8
Hi!

I still don't understand how do you retrieve the object reference. You get the class of the underlying object, not the object itself. Could you please explain? Thanks very much.


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