• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate HibernateTemplate.get() returns incorrect subclass instance

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've just run into an unexpected problem with hibernate: i make a HibernateTemplate.get(myClass.class, id) call to get back an instance of my object. While testing for incorrect results, i tried the id of a sibling subclass...and it returned an object of the incorrect type! This is most strange to me. Hopefully it is just a problem with my hibernate config file.

The platform:
* hibernate 2
* spring
* java5
* tc 5.5

My eg class structure:

A child has a unidirectional association to the parent.

My hibernate config snippet:


And the resulting table:


So, why is in in my code when I call HibernateTemplate.get(edu.moose.Parent, 3) it returns anything at all...and it is even cast as a Parent. The same holds true for HibernateTemplate.get(edu.moose.Child, 2)!

I should note that calling HibernateTemplate.get(edu.moose.Person) returns a list of correctly sub-typed objects.

Thanks for your help...hopefully this is plain to someone. At the moment I am considering moving to a subclass-joined hibernate configuration with the idea that classes being in different tables will make a difference.

EDIT: I see from the debug hibernate logs that the SQL does not specificy a discriminator either as a retuned parameter or in the where clause. So, the class parameter in .get() is apperently only used for creating the return type. Humph.

Stu

[ October 18, 2006: Message edited by: Stu Thompson ]

[ October 19, 2006: Message edited by: Stu Thompson ]
[ October 19, 2006: Message edited by: Stu Thompson ]
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, problem sovled...the cowardly way...i upgraded to Hibernate 3 and this is no longer a problem. Lucky me that I can do so in my project.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic