-->
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.  [ 11 posts ] 
Author Message
 Post subject: Class-type is different than expected in result
PostPosted: Mon Aug 08, 2005 6:27 am 
Newbie

Joined: Mon Aug 08, 2005 6:04 am
Posts: 5
Location: Germany
Hello,

we have the following problem when using hibernate with jboss: We execute a query with session.createQuery("..."). Afterwards we iterate the result. When testing for the instance's classname we get a bad result. Our instance in the result has the class-name 'services.Address$$EnhancerByCGLIB$$3a12e60b' instead of 'services.Address'.
If I use the same code only with java and hibernate without the webservices it works well.
Can anyone help?
thanx a lot, Kerstin.


Hibernate version: 3.0
Code between sessionFactory.openSession() and session.close():

Code:
if ((session != null) && session.isOpen()) {
  Query q = session.createQuery("from services.Address where Bgrid = 1");
  Iterator it = q.iterate();
  while (it.hasNext()){
    Object bgr = it.next();
    if (bgr instanceof Address) {
      result += ((Address)bgr).getBezeichnung();
    } else {
      result += bgr.toString();
      result += " Class: " + bgr.getClass().getName();
    }
   session.close();
  }

Name and version of the database you are using:
MySQL 4.1.13[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 6:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
don't use q.iterate() here. Use q.list().iterator() instead. Read the docs for the differences.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 7:05 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
If you want to get original entity class you can use Hibernat.getClass() or HibernateProxyHelper.getClassWithoutInitializingProxy() methods.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 7:35 am 
Newbie

Joined: Mon Aug 08, 2005 6:04 am
Posts: 5
Location: Germany
Thanks so far!
Now I get the right Classname (services.Address) but the test with
Code:
(bgr instanceof Adress)
or
HibernateProxyHelper.getClassWithoutInitializingProxy(bgr) == Address.class

still fails. What's wrong?

Thanks, Kerstin.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 8:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well what do you mean by "get the right Classname"?

I fail to see how bgr.getClass.getName() would be "Address" but that if ( bgr instanceof Address ) would fail, aside from classloader issues.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 11:25 am 
Newbie

Joined: Mon Aug 08, 2005 6:04 am
Posts: 5
Location: Germany
Hi Steve,
in my opinion, with the select in my first code-snippet with
Code:
Query q = session.createQuery("from services.Address where Bgrid = 1");

I retrieve Objects with class-type 'services.Address' in my result. While iterating the queries resultlist the iterated items should be of type services.Address. With retrieving the iterated item (Object bgr) and calling bgr.getClass().getName() I expect to get the String 'services.Address'.
So far I retrieve this String, but the test 'if (bgr instanceof services.Address)' fails.
You mentioned classloader issues. Do you have any idea what can go wrong with this in my case?
I'm sorry, but I'm not yet very familiar with hibernate and Webservices.


Top
 Profile  
 
 Post subject: classloaders
PostPosted: Mon Aug 08, 2005 1:26 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
bgr instanceof services.Address could fail even if bgr IS the instance of servicess.Address :)
This is the situation when Java classloading architecture rises its ugly head: according to the spec an object is the instance of a class only if they have exactly the same classloader.
In other words: Java runtime alters Java language semantic.
In the situation it is most likely that two different classloaders have instantiated class from the same source (jar file or classes directory) and instance has expected name “services.Address”, but instanceof fails.

A workaround would be to compare strings:
If( Address.class.getName().equals( o.getClass().getName() )


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:56 am 
Newbie

Joined: Mon Aug 08, 2005 6:04 am
Posts: 5
Location: Germany
Thanks for the advice to test equality of Classnames. This test works fine, but afterwards I still have the same problem: I want to read the attributes with getters from my object. For this I need to cast my object to services.Address, but this of course still fails.
Is there any possibility to set a classloader that is used for all class? Or do I have to configer JBoss/Hibernate somewhere to do so?
Someone suggested me to use Interfaces - does anyone know if/how it works?

Thanks a lot, Kerstin.


Top
 Profile  
 
 Post subject: -solved ?
PostPosted: Mon Nov 14, 2005 6:14 am 
Newbie

Joined: Mon Nov 14, 2005 6:04 am
Posts: 1
Hello Kerstin,

Did you eventually find out how to cast the object ?
I am having the same issue and cannot find out what to do with the

my.business.Test$$EnhancerByCGLIB$$3a12e60b

object !


Thanks very much for your reply !!!

/Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 6:29 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Code:
public static Object narrow(Object o){

  if( o instanceof HibernateProxy){

   return ((HibernateProxy)o).
                   getHibernateLazyInitializer().getImplementation();

  }else{
      return o;
  }


}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 3:29 pm 
Newbie

Joined: Mon Aug 08, 2005 6:04 am
Posts: 5
Location: Germany
Hi,

in the meantime I solved the problem by myself. The fault was, that we had an .war- and .har-File that contained the same model objects. Thus we had the same Klassnames, but because of different classloaders it wasn't the same class. When we put out model objects in a jar-file in the lib-folder (our first approach, probably not the right solution) it worked. I think, the right solution would be to create an .ear-file which contains the model objects as well as all service- and hibernate-files.

Perhaps someone has done the same mistakes.


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