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.