-->
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.  [ 6 posts ] 
Author Message
 Post subject: Question about many-to-one relation and polymorphism
PostPosted: Tue Sep 09, 2008 8:10 am 
Newbie

Joined: Tue Sep 09, 2008 7:53 am
Posts: 3
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:

java.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


Top
 Profile  
 
 Post subject: Re: Question about many-to-one relation and polymorphism
PostPosted: Tue Sep 29, 2009 3:56 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi gewuerzgurke,

I also had problems with this issue. I searched quite a while for a proper solution but found only the interface-way.

I managed to handle it by implementing an interface for each class in the inheritance tree and used them for mapping the proxy-attribute. The entity class and the generated proxy class therefore inherit from the same parent interface to which you can then cast the elements of your result sets. Works out fine so far.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Question about many-to-one relation and polymorphism
PostPosted: Tue Sep 29, 2009 4:19 pm 
Newbie

Joined: Tue Sep 09, 2008 7:53 am
Posts: 3
Hi froestel,

I've fixed this Problem by enabling the bytecode instrumentation ;) let me know if you want to See the solution.

Best regards,
Alex


Top
 Profile  
 
 Post subject: Re: Question about many-to-one relation and polymorphism
PostPosted: Tue Sep 29, 2009 5:41 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi gewuerzgurke,

I tried that, too, but for some reason it didn't work. But I didn't bother with it.

What are your performance experiences with bytecode instrumentation? Is it "expensive"?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Question about many-to-one relation and polymorphism
PostPosted: Wed Sep 30, 2009 8:19 am 
Newbie

Joined: Tue Sep 09, 2008 7:53 am
Posts: 3
No I haven't seen any differences in runtime-performance. The only con is that you've to implement this bytecode instrumentation into the build process, that was tricky :)


Top
 Profile  
 
 Post subject: Re: Question about many-to-one relation and polymorphism
PostPosted: Tue Nov 10, 2009 9:15 am 
Newbie

Joined: Wed Sep 09, 2009 8:49 am
Posts: 3
Hi,

Can you please explain this bytecode instrumentation trick to avoid defining interfaces?

Thanks.


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