-->
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.  [ 8 posts ] 
Author Message
 Post subject: load() is not polymorphic?
PostPosted: Fri Jan 02, 2004 1:55 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
I encountered some unexpected behavior when loading objects within a class heirarchy (hibernate 2.1). For example:

Animal
-->Cat
-->Dog

If I run a query like:

"FROM Animal as animal"

I will get back all Cats and Dogs in this table, as expected. Also as expected, each object returned from this query will be an instance of either a Cat or Dog. So far so good.

But, if I do the same thing using "load", the results don't seem intuitive.

Animal animal = (Animal) session.load(Animal.class, 1234);

If the row by the ID of 1234 is actually a Dog, I would expect that my "animal" instance returned by "load" to be an instance of Dog. But this is not the case! Trying to cast "animal" to Dog throws a ClassCastException. "animal" is only an instance of "Animal".

Therefore, it appears to me that using the query interface versus "load" works very differently in regards to polymorphism. Queries support polymorphism while load() does not. A more expected behavior of load in this case might be:

- Actually have the Animal instance returned from load be an instance of Dog, since in the database, row 1234 is a Dog. This would be inline with the behavior of the query interface and polymorphism.

In the meantime, to solve the actual problem I'm running in to, I'll use the query interface instead of "load". Seems weird, but it works.

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
As written many times - both on forums and documentation - load() does not hit the database (making it very usefull for making associations without loading data), so it has no way of knowing what the concrete type is!

Thus either use find() or get() do such things!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hm, just out of interest, but if I load the object the first time, load has to hit the database unless I use a proxy ... so couldn't it instatiate the correct type?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 2:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Does it not instantiate the correct type if you are not using a proxy ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 2:50 pm 
Beginner
Beginner

Joined: Wed Oct 01, 2003 3:47 pm
Posts: 44
Thank you, Max. I switched to "get()" as you suggested, and polymorphism now works as expected.

Thanks,

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 3:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Where you actually using a proxy?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 3:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just tried this without proxy, and it works here for me. The returned object is of the subclass type.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2004 3:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
rdewell wrote:
Thank you, Max. I switched to "get()" as you suggested, and polymorphism now works as expected.

Thanks,

Ryan


and as get() and load() is using the same code to do the actual loading internally then i'm quite sure you have marked you object as lazy=true or proxy=sameclassname.

_________________
Max
Don't forget to rate


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