-->
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.  [ 1 post ] 
Author Message
 Post subject: Get proxy for parent class instead of the object's real clas
PostPosted: Sun Oct 09, 2005 7:53 am 
Newbie

Joined: Sun Oct 09, 2005 6:36 am
Posts: 19
I have a simple class hierarchy with a parent and a subclass. When i use session.load(parentclass, id) with id being the id of a subclass instance i get the correct object but the proxy only extends parent class. So when i try to cast the object to subclass later in the code i get a ClassCastException


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

<hibernate-mapping package="test">

    <class name="ParentClass" table="parent" lazy="true" abstract="true">         
        <id name="myID" column="id" access="field" type="string">
      </id>
      <version column="version" name="myVersion" access="field"/>     
      <property name="myName" column="name" type="string" access="field"/>
    </class>
   
   <joined-subclass name="SubClassA" extends="ParentClass" table="subclass_a" lazy="true">           
      <key column="id"/>         
   </joined-subclass>   
   
</hibernate-mapping>

The java classes look like this:

Code:
public class ParentClass
{
   String myID;
   int myVersion;
   String myName;
   
   protected ParentClass()
   {     
   }
   
   public ParentClass(String aName)
   {
      myID = aName;
      myName = aName;
   }
}

public class SubClassA extends ParentClass
{
   protected SubClassA()
   {     
   }
   
   public SubClassA(String aID)
   {
      super(aID);
   }
}

And the code doesn't do anything special either

HibernateUtil.getSession().save(new SubClassA("test"));

and in another transaction

Object loResult = loSession.load(ParentClass.class, "test");

I would expect that i could cast loResult to SubClass but i can't because the proxy just extends ParentClass. When i use loSession.load(SubClass.class, "test") everything works as expected of course.

My real code consists of users/groups that share a base class that contains the userID, permissions, ... and a simple get(id) method that just gets the hibernate session and uses load(...). So there are lots of ways to solve my problem. But as i just started using Hibernate i'd really like to know why this happens and whether this is a feature, "correct by definition" or a bug in my code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.