-->
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: instanceof and interface proxies
PostPosted: Tue Nov 04, 2003 10:57 am 
Newbie

Joined: Tue Nov 04, 2003 10:45 am
Posts: 2
Location: Paris, France
Suppose I have a base class, Cat, and two derived classes: DomesticCat and WildCat. I define proxy interfaces to these persistent classes in order to use lazy initialization: ICat, IDomesticCat and IWildCat.

Now when I query the base class, how can I find the most-derived class of the returned object?

Code:
Query q = session.createQuery("from Cat");
Iterator it = q.list().iterator();
while (it.hasNext()) {
    ICat cat = (ICat)it.next();
    if (cat instanceof IWildCat)
        // This is true for all objects !!!!!
    if (cat instanceof IDomesticCat)
        // This is true for all objects !!!!!
}


Apparently instanceof doesn't work on proxy interfaces. Is there an alternative? Am I doing something wrong?

Thanks in advance,
Raph


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 11:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummm this is sort of correct, except that find() never returns proxies, so perhaps your actual code is slightly different??

But yes, interface proxies do implement all derived interfaces. Either

(a) use a concrete proxy instead of the interface proxy
(b) call Hibernate.getClass()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 11:20 am 
Newbie

Joined: Tue Nov 04, 2003 10:45 am
Posts: 2
Location: Paris, France
gavin wrote:
Ummm this is sort of correct, except that find() never returns proxies, so perhaps your actual code is slightly different??


In my case, some of the objects returned by this query may have already been loaded in the current session using load(). I noticed that only these objects were being proxied while the other ones were of the actual persistent class.

Quote:
(b) call Hibernate.getClass()


This is exactly what I was looking for. Thanks a million!!

Raph


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 11:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh yeah, sure. You confused me with "this is true for all objects".


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.