-->
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: Retrieving subclass object causes ClassCastException
PostPosted: Fri Nov 02, 2007 7:50 am 
Newbie

Joined: Mon Oct 01, 2007 12:09 pm
Posts: 2
There is some fundamental thing about inheritance mapping that I don't get. I'm creating a generic attribute system, which should be able to store strings, floating point numbers and integers.
The abstract base class is called AttributeValue, extended by StringAttributeValue, DoubleAttributeValue and LongAttributeValue.
The test cases attempt to store and retrieve values of all three types. Storing the values goes fine, everything looks OK when I inspect the database.

I'm retrieving the values by calling session.get(AttributeValue.class, id), which works fine for String values, but retrieving Long and Double values causes ClassCastException (see stacktrace below).
Retrieveing values by using concrete class names, like session.get(DoubleAttributeValue.class, id), also works. However, to retrieve all attributes I would then have to
repeat session.get for all concrete classes and aggregate the results, which seems counter-intuitive.

So the question is: How can I make session.get(AttributeValue.class, id) return objects from the subclasses?

Hibernate version:
3.1rc2

Mapping documents:
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>
    <class name="com.idium.ips.attributes.baseattributetypes.AttributeValue" table="ATTRIBUTEVALUES" abstract="true">

        <id name="id" column="ID">
            <!-- The identity generator strategy is not allowed in union subclass inheritance,
            indeed the primary key seed has to be shared accross all unioned subclasses
            of a hierarchy.
            http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html section 9.1.5 -->
            <generator class="sequence"/>
        </id>

        <union-subclass name="com.idium.ips.attributes.baseattributetypes.StringAttributeValue" table="STRINGATTRIBUTEVALUES">
            <property name="value" column="VALUE" type="string"/>
        </union-subclass>

        <union-subclass name="com.idium.ips.attributes.baseattributetypes.DoubleAttributeValue" table="DOUBLEATTRIBUTEVALUES">
            <property name="value" column="VALUE" type="double"/>
        </union-subclass>

        <union-subclass name="com.idium.ips.attributes.baseattributetypes.LongAttributeValue" table="LONGATTRIBUTEVALUES">
            <property name="value" column="VALUE" type="long"/>
        </union-subclass>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
    public void testStringValue() throws Exception {
        AttributeValue<String> value = prepare("Tjorven", NOR_LOCALE, StringAttributeValue.class);
        doAssertions(StringAttributeValue.class, value);
    }

    public void testDoubleValue() throws Exception {
        AttributeValue<Double> value = prepare(3.14, NOR_LOCALE, DoubleAttributeValue.class);
        doAssertions(DoubleAttributeValue.class, value);
    }

    public void testLongValue() throws Exception {
        AttributeValue<Long> value = prepare((long) 2, NOR_LOCALE, LongAttributeValue.class);
        doAssertions(LongAttributeValue.class, value);
    }

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    private <T> AttributeValue<T> prepare(T value, Locale locale, Class<? extends AttributeValue<T>> valueClass) throws Exception {
        AttributeValue<T> attributeValue = ((Class<? extends AttributeValue<T>>) valueClass).newInstance();
        attributeValue.setValue(value);
        attributeValue.setLocale(locale);
        getSession().save(attributeValue);

        getSession().getTransaction().commit();
        getSession().beginTransaction();

        return attributeValue;
    }

    private <T> void doAssertions(Class<? extends AttributeValue<T>> valueClass, AttributeValue<T> attributeValue) {
/** The next line fails with ClassCastException if (valueClass == LongAttributeValue.class || valueClass == DoubleAttributValue.class) **/
        AttributeValue retrievedValue = (AttributeValue) this.getSession().get(AttributeValue.class, attributeValue.getId());

        assertEquals("Retrieved value is not of expected class,", valueClass, retrievedValue.getClass());
        assertEquals("Retrieved value is wrong,", ((AttributeValue) attributeValue).getValue(), retrievedValue.getValue());
        assertEquals("Retrieved value does not have expected locale,", attributeValue.getLocale(), retrievedValue.getLocale());
    }


Full stack trace of any exception that occurs:
Code:
org.hibernate.exception.GenericJDBCException: could not load an entity: [com.idium.ips.attributes.baseattributetypes.AttributeValue#2]
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1796)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:93)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:81)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2729)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
   at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:809)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:749)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:742)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:271)
   at $Proxy0.get(Unknown Source)
   at com.idium.ips.attributes.baseattributetypes.AttributeTypesTestInSuite.doAssertions(AttributeTypesTestInSuite.java:49)
   at com.idium.ips.attributes.baseattributetypes.AttributeTypesTestInSuite.testDoubleValue(AttributeTypesTestInSuite.java:30)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
Caused by: java.sql.SQLException: S1000 General error java.lang.ClassCastException: java.lang.Double in statement [select attributev0_.ID as ID8_0_, attributev0_.LOCALE as LOCALE8_0_, attributev0_.VALUE as VALUE9_0_, attributev0_.VALUE as VALUE10_0_, attributev0_.VALUE as VALUE11_0_, attributev0_.clazz_ as clazz_0_ from ( select LOCALE, VALUE, ATTRIBUTE_ID, ID, 1 as clazz_ from STRINGATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 2 as clazz_ from DOUBLEATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 3 as clazz_ from LONGATTRIBUTEVALUE ) attributev0_ where attributev0_.ID=?]
   at org.hsqldb.jdbc.Util.throwError(Unknown Source)
   at org.hsqldb.jdbc.jdbcPreparedStatement.executeQuery(Unknown Source)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:137)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1676)
   at org.hibernate.loader.Loader.doQuery(Loader.java:662)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1782)
   ... 37 more


Name and version of the database you are using:
HSQLDB 1.8.0.1

The generated SQL (show_sql=true):
Code:
select attributev0_.ID as ID8_0_, attributev0_.LOCALE as LOCALE8_0_, attributev0_.VALUE as VALUE9_0_, attributev0_.VALUE as VALUE10_0_, attributev0_.VALUE as VALUE11_0_, attributev0_.clazz_ as clazz_0_ from ( select LOCALE, VALUE, ATTRIBUTE_ID, ID, 1 as clazz_ from STRINGATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 2 as clazz_ from DOUBLEATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 3 as clazz_ from LONGATTRIBUTEVALUE ) attributev0_ where attributev0_.ID=?



Debug level Hibernate log excerpt:
Code:
Hibernate: insert into AttributeSet (name, ID) values (?, null)
Hibernate: call identity()
Hibernate: insert into ATTRIBUTE (ATTRIBUTESET_ID, name, ID) values (?, ?, null)
Hibernate: call identity()
Hibernate: select next value for hibernate_sequence from dual_hibernate_sequence
Hibernate: insert into DOUBLEATTRIBUTEVALUE (LOCALE, VALUE, ID) values (?, ?, ?)
Hibernate: select attributev0_.ID as ID8_0_, attributev0_.LOCALE as LOCALE8_0_, attributev0_.VALUE as VALUE9_0_, attributev0_.VALUE as VALUE10_0_, attributev0_.VALUE as VALUE11_0_, attributev0_.clazz_ as clazz_0_ from ( select LOCALE, VALUE, ATTRIBUTE_ID, ID, 1 as clazz_ from STRINGATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 2 as clazz_ from DOUBLEATTRIBUTEVALUE union select LOCALE, VALUE, ATTRIBUTE_ID, ID, 3 as clazz_ from LONGATTRIBUTEVALUE ) attributev0_ where attributev0_.ID=?


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