-->
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.  [ 6 posts ] 
Author Message
 Post subject: ClassCastException after joined-subclass load
PostPosted: Mon Oct 03, 2005 1:19 am 
Newbie

Joined: Sun Oct 02, 2005 11:39 pm
Posts: 3
JDK 1.5.0_04

Hibernate version: 3.0.5

Mapping documents:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="au.gov.vic.ngv.shop.admin.model.Person">
    <id name="ID">
      <generator class="increment"/>
    </id>
    <property name="name"/>
    <joined-subclass name="au.gov.vic.ngv.shop.admin.model.Manager" lazy="false">
      <key column="ID"/>
      <property name="bonus"/>
    </joined-subclass>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
      
Manager manager = (Manager) sess.load(Person.class,new Long(1));


Full stack trace of any exception that occurs:

java.lang.ClassCastException: au.gov.vic.ngv.shop.admin.model.Person$$EnhancerByCGLIB$$36a08cbc
at au.gov.vic.ngv.shop.admin.dao.TestProductDAO.testLoadPerson(TestProductDAO.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Name and version of the database you are using: SQL Server 2000


I can't understand why this is not working and I've searched everywhere. Can anyone see if I may have missed anything?

If I use Manager.class in the load() method instead of the superclass:

Code:
Manager manager = (Manager) sess.load(Manager.class,new Long(1));


it works fine, but this means I can't load objects polymorphically.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 1:50 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
think about this. every dog is a mammal, but not every mammal is a dog.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 2:10 am 
Newbie

Joined: Sun Oct 02, 2005 11:39 pm
Posts: 3
dennisbyrne wrote:
think about this. every dog is a mammal, but not every mammal is a dog.


Hi Dennis,

I should still be able to load a Person object without knowing its runtime class and then cast it later. The following example comes straight from the hibernate manual:

Code:
DomesticCat pk = (DomesticCat) sess.load( Cat.class, new Long(pkId) );


I'm pretty sure that this does the same thing that I am attempting.

Thanks anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 2:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You must use session.get(), or simply never write such evil non-object oriented stuff. Real OO code uses polymorphism, not typecasts.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 3:02 am 
Newbie

Joined: Sun Oct 02, 2005 11:39 pm
Posts: 3
gavin wrote:
You must use session.get(), or simply never write such evil non-object oriented stuff. Real OO code uses polymorphism, not typecasts.


Hey, just copying the examples in the manual.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 3:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, the example is wrong.


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