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]