-->
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: Proxy object issue
PostPosted: Tue Apr 13, 2004 10:20 am 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
I am using Hibernate 2.1.2.

The mapping is question is the following:

Code:
<hibernate-mapping>
  <class name="com.foo.bar.beans.user.User" table="USERS" proxy="com.foo.bar.beans.user.User" dynamic-update="false" dynamic-insert="false" mutable="true">
    <id name="userId" column="USERID" type="java.lang.Long" unsaved-value="null">
      <generator class="sequence">
        <param name="sequence">SEQ_USERID</param>
      </generator>
    </id>
    <property name="firstName" type="java.lang.String" update="true" insert="true" column="FIRSTNAME" not-null="true"/>
    ...
    ...
  </class>
</hibernate-mapping>


This is a sample section of code
Code:
  Long userId = new Long(1);
  UserDAO userDAO = new UserDAO();
  User user = userDAO.searchByPrimaryKey(userId);
  logger.debug("Class: " + user.getClass());
  if (user.getClass().isInstance(new User()))
  {
    logger.debug("Result is an instance of the User object.");
  }
  else
  {
    logger.debug("Result is NOT an instance of the User object.");
  }


The following is the output
Code:
Class: com.foo.bar.beans.user.User$$EnhancerByCGLIB$$f0dddb0d
Result is NOT an instance of the User object.


I am guessing that this is a result of Proxy setting. Does anyone know how I can get the result to be an instance of my User object. I have tried to typecast it but it makes no difference.

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 12:00 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Quote:
I am guessing that this is a result of Proxy setting. Does anyone know how I can get the result to be an instance of my User object. I have tried to typecast it but it makes no difference.

Why do you think result is not an instance of your User class ? User class is a superclass for com.foo.bar.beans.user.User$$EnhancerByCGLIB$$f0dddb0d , so result is an instance of User class, but User instance is not an instace of result class. Is it a problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 12:09 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Quote:
Why do you think result is not an instance of your User class ? User class is a superclass for ?



The problem is that when I pass the User object into a separate method and do the following

Code:
object.getClass().isInstance(new User())


it returns a false. Consequently, I can't process further in my code. I do similar processing in other parts of my code and it works fine. The User object is the only one that is proxy loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 12:23 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Yes, proxy is a subclass.
"User.class.isInstance(myObject)" must return true.
"if(myObject instanceof User)" is a right way too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 1:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
or if you want a dynamic instanceof then use isAssignableFrom instead

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 3:41 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Both isAssignableFrom and instanceof worked. Thank you very much. I am still surprised that isInstance doesn't work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 4:26 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
gpani wrote:
Both isAssignableFrom and instanceof worked. Thank you very much. I am still surprised that isInstance doesn't work.


JAVA compiler implements "instanceof" as "isInstance", but "isInstance" and
"isAssignableFrom" sometimes confuses, it is more clear to use "instanceof" operator.


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.