Hi everyone,
I'm using the latest hibernate version (=3.3.0 SP1), PostgreSQL (=8.2.4) and Spring (=2.5.5).
The problem that stresses me is about a many-to-one relation that points to a entity that has
a inheritance mapping:
Code:
<class name="sample.Parent" table="parent">
<meta attribute="scope-class">
public abstract
</meta>
<meta attribute="implements">
FooInterface
</meta>
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer" column="parent_id">
<meta attribute="scope-set">protected</meta>
<generator class="increment"/>
</id>
<property name="someParentProperty"/>
<joined-subclass name="sample.Child1" table="child_one">
<key column="child_one_id"/>
<property name="someChildProperty"/>
</join-subclass>
<joined-subclass name="sample.Child2" table="child_two">
<key column="child_two_id"/>
<property name="someOtherChildProperty"/>
</join-subclass>
</class>
The mapping that refers to the inheritance mapping:
Code:
<class name="sample.Shop" table="shop">
<cache usage="read-write" />
<id name="id" type="java.lang.Integer" column="shop_id">
<meta attribute="scope-set">protected</meta>
<generator class="increment"/>
</id>
<many-to-one name="parent" class="sample.Parent" column="parent_id" not-null="false" />
</class>
Now when I get an Instance (managed by hibernate) from sample.Shop and try to cast the result of
getParent() to sample.Child1 (because I know that the instance must be of the type) the following
error is thrown:
j
ava.lang.ClassCastException: sample.Parent_$$_javassist_28 cannot be cast to sample.Child1
at com.infinconsystems.tests.spring.HibernateTest.testRiskAnswers(HibernateTest.java:22)
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:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
I've already found some posts facing this problem.
So two questions:
a) Is the one and only solution to use proxy="" at the joined-subclass and the superclass?
b) Is hibernate's codegen able to produce an interface and an implementing class that I could use at a) ?
c) When there is another way, how it's implemented :) ?
Best regards,
Alexander Müller