-->
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.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException with proxy
PostPosted: Tue Oct 09, 2007 10:43 am 
Newbie

Joined: Fri Mar 23, 2007 4:47 am
Posts: 16
Hi All

I have a class and 2 joined-subclasses. The parent class is AbstractDatasource. The 2 sub classes are JDBCDatasource and BeanDatasource. When I retreive a list of AbstractDatasource using the foloowing code

Code:
DetachedCriteria detachedCriteria = DetachedCriteria.forClass( AbstractDatasource.class );

List objects = getHibernateTemplate( ).findByCriteria( detachedCriteria );


I discover that the objects arent of the correct class. I know this because I have 1 BeanDatasource in the database and when I try to convert the BeanDatasource using the following code:

Code:
(BeanDatasource)objects.get(0);


but I get a ClassCastException. This I beleive has to do with some kind of proxy that is used by Hibernate. I was wandering how can I turn of this proxy, that is if it's the problem or is there something else wrong?

When I do a System.out.println( objects.get(0) ) I see this
com.mycompany.AbstractDatasource$$EnhancerBYCGLIB$$ but this should show com.mycompany.BeanDatasource.

Thanks in advance,
Martyn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 6:06 am 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
Proxy objects will extend the type you ask for in the session.get() operation. So, to cast it you will need to get hold of the unproxied object inside. One way to do this is the following:

class Parent
{
public Child asChild()
{
return (Child)this;
}
}


...
Child c = session.get(Parent.class, id);

Mind that the same goes for instanceof. You could make isA methods for that (e.g. boolean isA(Class type).


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