-->
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.  [ 4 posts ] 
Author Message
 Post subject: Class Cast Exception while Using <subclass> inheritanc
PostPosted: Wed Nov 02, 2005 9:57 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Hi,
I'm trying to implement the inheritance using the "table per class heirarchy" strategy and getting a Class Cast Exception. Not sure what I'm missing. Here's my table structure:

create table LIST_OF_VALUES_MASTER
(
value_id smallint not null,
value_group varchar(2) not null,
value_desc varchar(15),
primary key (value_id,value_group)
);

VALUE_ID|VALUE_GROUP|VALUE_DESC
-------------------------
1 |1 |Sev-1
2 |1 |Sev-2
3 |1 |Sev-3
4 |1 |Sev-4
1 |2 |Production
2 |2 |Adoption

Hibernate version: 3.0

Mapping documents:
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.e3tickets.ListOfValuesMaster" table="LIST_OF_VALUES_MASTER" schema="APP">
<id name="valueId" type="integer">
<column name="VALUE_ID" />
<generator class="assigned" />
</id>
<discriminator column="VALUE_GROUP" type="string"/>
<property name="valueDesc" type="string">
<column name="VALUE_DESC" length="15" />
</property>
<subclass name="com.e3tickets.Severity" discriminator-value="1">
<property name="valueGroup" type="integer" insert="false" update="false">
<column name="VALUE_GROUP"/>
</property>
</subclass>
<subclass name="com.e3tickets.E3Environment" discriminator-value="2">
<property name="valueGroup" type="integer" insert="false" update="false">
<column name="VALUE_GROUP"/>
</property>
</subclass>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Transaction tx = s.beginTransaction();
IssueMaster issue = new IssueMaster();
Severity severity = new Severity();
severity = (Severity)s.get(Severity.class,new Integer(1));

E3Environment env;
E3Environment obj = s.get(E3Environment.class,new Integer(1));


Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.ClassCastException: com.e3tickets.Severity

Name and version of the database you are using:
Derby
The generated SQL (show_sql=true):
select severity0_.VALUE_ID as VALUE1_0_, severity0_.VALUE_DESC as VALUE3_2_0_, severity0_.VALUE_GROUP as VALUE2_2_0_ from APP.LIST_OF_VALUES_MASTER severity0_ where severity0_.VALUE_ID=? and severity0_.VALUE_GROUP='1'

Definition of Severity and Environment class

public class E3Environment extends ListOfValuesMaster {
public static final int ENV_PROD =1;
public static final int ENV_ADOP =2;
}

public class Severity extends ListOfValuesMaster {

public static final int SEV_1=1;
public static final int SEV_2=2;
public static final int SEV_3=3;
public static final int SEV_4=4;

}

public class ListOfValuesMaster implements java.io.Serializable {

// Fields

private int valueId;
private int valueGroup;
private String valueDesc;

...< getters and setters>
}


Top
 Profile  
 
 Post subject: Missing discriminator-value
PostPosted: Wed Nov 02, 2005 10:32 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I've never seen an explicit reason for this, but every example I've ever seen, and accordingly every implementation I've written, has had a discriminator-value for the class as well as all subclasses. Add discriminator-value="0" to the ListOfValuesMaster class in your map file and see how that works.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 11:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I don't understand. How can a single row (VALUE_ID=1) simultaneously have VALUE_GROUP=1 and VALUE_GROUP=2?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 12:03 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Ooo well spotted, the lack of a code tag hid that from me. A composite-id is needed for that mapping.


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