-->
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.  [ 2 posts ] 
Author Message
 Post subject: Search by example of non-abstract super class instance
PostPosted: Mon Aug 28, 2006 12:34 pm 
Newbie

Joined: Mon Aug 28, 2006 12:13 pm
Posts: 4
I think I have found a bug, but just to be sure that it is not known before I will post it in this forum first.

The situation is the following: I have one base abstract class called Vehicle
and stored in VEHICLE table. All its subclass instances are also stored in the same table based on discriminator. Imagine the following mapping:

Code:
<hibernate-mapping package="test.hibernate.inheritance">

    <class
        abstract="true"
        name="Vehicle"
        select-before-update="true"
        dynamic-update="true"
        table="VEHICLE">

        <id
            name="id"
            type="java.lang.Long">
            <column name="ID" />
            <generator class="sequence">
                <param name="sequence">VEHICLE_ID_SEQ</param>
            </generator>
        </id>

        <discriminator
            column="VEHICLE_TYPE"
            not-null="true"
            force="true"
            type="java.lang.String"
            length="20" />

        <property
            name="name"
            type="string">
            <column
                name="ACTIVITY_TYPE"
                length="20" />
        </property>

        <subclass
            discriminator-value="CAR"
            name="Car"
            extends="Vehicle">

            <property
                name="engine"
                type="java.lang.Double"
                column="ENGINE" />

        </subclass>

        <subclass
            discriminator-value="RSX"
            name="MazdaRSX"
            extends="Car">

            <property
                name="modification"
                type="java.lang.String"
                column="MODIFIER" />

        </subclass>

        <subclass
            discriminator-value="Neon"
            name="DodgeNeon"
            extends="Car">

            <property
                name="majorModel"
                type="java.lang.Long"
                column="MAJOR" />

            <property
                name="minorModel"
                type="java.lang.Long"
                column="MINOR" />

        </subclass>


    </class>

</hibernate-mapping>


The class Car is not abstract and allows custom car models to be stored
in the database.

When trying to search by example using Car class instance:

Code:
        Car carSearch = new Car();
        carSearch.setEngine(2.0);

        Criteria criteria = session.createCriteria(Car.class);
        criteria.add(Example.create(carSearch));
       
        List cars = criteria.list();


I'm getting ClassCastException:

Code:
java.lang.ClassCastException: test.hibernate.inheritance.Car
   at org.hibernate.criterion.Example.getEntityMode(Example.java:247)
   at org.hibernate.criterion.Example.toSqlString(Example.java:177)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:316)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:86)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1473)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
   at test.hibernate.inheritance.SearchTest.testInheritanceSearch(SearchTest.java:73)
   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)


I have a separate eclipse project that test only this situation with unit test. I can send it to someone who knows or have a clue what could be the problem :)


Best regards,
Andrey[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 1:47 pm 
Newbie

Joined: Mon Aug 28, 2006 12:13 pm
Posts: 4
Sorry, for bothering. My bad. I have found the solution to the problem.

The subclass definitions should be just put outside main class definition (Vehicle) as they form more complex inheritance tree, but by default Hibernate puts them to be an extentation of the main class instead of class
specified in "extends" attribute.

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.hibernate.inheritance">

    <class
        abstract="true"
        name="Vehicle"
        select-before-update="true"
        dynamic-update="true"
        table="VEHICLE">

        <id
            name="id"
            type="java.lang.Long">
            <column name="ID" />
            <generator class="sequence">
                <param name="sequence">VEHICLE_ID_SEQ</param>
            </generator>
        </id>

        <discriminator
            column="VEHICLE_TYPE"
            not-null="true"
            force="true"
            type="java.lang.String"
            length="20" />

        <property
            name="name"
            type="string">
            <column
                name="ACTIVITY_TYPE"
                length="20" />
        </property>

    </class>

[b]    <subclass
        discriminator-value="CAR"
        name="Car"
        extends="Vehicle">

        <property
            name="engine"
            type="java.lang.Double"
            column="ENGINE" />

    </subclass>

    <subclass
        discriminator-value="RSX"
        name="MazdaRSX"
        extends="Car">

        <property
            name="modification"
            type="java.lang.String"
            column="MODIFIER" />

    </subclass>

    <subclass
        discriminator-value="Neon"
        name="DodgeNeon"
        extends="Car">

        <property
            name="majorModel"
            type="java.lang.Long"
            column="MAJOR" />

        <property
            name="minorModel"
            type="java.lang.Long"
            column="MINOR" />

    </subclass>

[/b]
</hibernate-mapping>



Best regards,
Andrey


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