-->
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.  [ 5 posts ] 
Author Message
 Post subject: Polymorphic results from a property of a queried object
PostPosted: Tue Apr 03, 2007 5:16 pm 
Newbie

Joined: Tue Apr 03, 2007 4:38 pm
Posts: 3
I have a question related to the polymorphic queries.
I have a class hierarchy as the following:

Base class Cat
subclass AmericanCat extends Cat
subclass AsianCat extends Cat

I have another class called Master which has one to one relationship with the Cat using the catId foreign key

Hibernate version:
3.1

Name and version of the database you are using:
MS SQL Server 2005[/code]
Mapping documents:

Cat class hierarchy mapping:

Code:
<hibernate-mapping>
    <class name="Cat" table="Cat" schema="dbo" catalog="Test2">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
       
        <property name="type" type="java.lang.String">
            <column name="discriminator" length="50" />
        </property>

        <joined-subclass name="AmericanCat" table="AmericanCat">
              <key column="catId" />
              <property name="americanName" type="java.lang.String" column="americanName"/>
        </joined-subclass>

        <joined-subclass name="AsianCat" table="AsianCat">
              <key column="catId" />
              <property name="asianPlace" type="java.lang.String" column="asianPlace"/>
        </joined-subclass>     
    </class>



The Master class mapping:

Code:
    <class name="Master" table="Master" schema="dbo" catalog="Test">
        <id name="masterId" type="java.lang.Integer">
            <column name="masterId" />
            <generator class="native" >
            </generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="10" not-null="true" />
        </property>     
       
        <many-to-one name="cat" column="catId" unique="true" not-null="true"/>
           
    </class>



When I perform a query like:

Code:

List masters = session.createQuery("from Master master where master.masterId='1'").list();
Master master = (Master)masters.get(0);
Cat cat = master.getCat();

System.out.println(cat.getId()); // This works fine.

//The following line generates a class cast excpetion although the selected
// cat is indeed of type AmericanCat.
System.out.println( ((AmericanCat)cat).getAmericanName());



I tested the polymorphic query using the "from Cat" and it works perfectly. The returned Cat super class can be casted to its original super class without problems . However, when I select the Master object, the superclass Cat property does not know its original subclass.

Any ideas if I am missing something or if there is any work around ?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 7:06 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Maybe this is a proxy problem

Using hibernate proxy for classes (which is the default behaviour) with inheritance can lead to some problems but this is normally relative to instanceof tests.

Can you debug your app to see the class of your cat. Is it a proxy or not ? Are your sure this cat is an american cat ?

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 8:14 am 
Newbie

Joined: Tue Apr 03, 2007 4:38 pm
Posts: 3
Thanks scesbron for the proxy hint.

I checked through the debugger and the returned cat reference was of type "Cat$$EnhancerByCGLIB$$28875802". It looks like a proxy as you have said.

Any idea how can we overcome this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 10:05 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Try setting lazy="false" on your cat mapping file

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 10:25 am 
Newbie

Joined: Tue Apr 03, 2007 4:38 pm
Posts: 3
Thanks for the answer.
I have given you the point of this post.

Actually, I tried specifying the "lazy=false" at the relationship between the Master and the Cat in the Master mapping file and it worked as well.

I think this will disable the lazy fetching only for the Cats which are associated with the Masters instead of disabling it for all the Cats.

Thanks Again for the support


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