-->
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: load() and get() behaviour
PostPosted: Tue Feb 05, 2008 6:34 am 
Newbie

Joined: Wed Dec 27, 2006 10:03 am
Posts: 19
Greetings,

I have a question on polymorphic criteria in Hibernate. Say, we have class hierarchy of three classes:


Code:
abstract class ModelObject {}
class Concrete1 extends ModelObject{}
class Concrete2 extends ModelObject{}


And we map those guys like this:

Code:
<hibernate-mapping
>
    <class
        name="model.ModelObject"
        table="model_object"
        proxy="model.ModelObject"
        polymorphism="implicit"
        mutable="true"
    >


        <discriminator
            column="object_type"
            type="string"
        />

        <subclass
            name="model.Concrete1"
            discriminator-value="c1"
        >

            <join table="concrete1">
                <key
                    column="v_id"
                />
        />

            </join>


        </subclass>
        <subclass
            name="model.Concrete2"
            discriminator-value="c2"
        >

            <join table="concrete2">
                <key
                    column="v_id"
                />

        />

            </join>


        </subclass>
</hibernate-mapping>

Querying the ModelObjects this way:
Code:
final ModelObject mo = (ModelObject) getSession().get(ModelObject.class, 1);

if(mo instanceof Concrete1) {

   ....

}


works just fine. On the other hand,
Code:
final ModelObject mo = (ModelObject) getSession().load(ModelObject.class, 1);


returns a dynamic proxy object, which cannot be cast to Concrete1 or Concrete2!


Can someone explain why this happens? Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 8:54 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
See this, I think it explains the difference well:

http://forum.hibernate.org/viewtopic.php?t=939273&sid=4add2400bd0140c51116043cd3fd15dd


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 9:40 am 
Newbie

Joined: Wed Dec 27, 2006 10:03 am
Posts: 19
Thanks for the pointer, but I'd like to hear why proxy object returned by

Code:
(ModelObject) getSession().load(ModelObject.class, 1);


cannot be cast to any of the ConcreteXXX types?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 9:44 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Sonar wrote:
Thanks for the pointer, but I'd like to hear why proxy object returned by

Code:
(ModelObject) getSession().load(ModelObject.class, 1);


cannot be cast to any of the ConcreteXXX types?


Ah, that's because the concrete types don't extend the dynamic proxy class.

Code:
                    ModelObject
                 /        |       \
        Concrete1     Concrete2       ModelObject$$etc



Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 10:21 am 
Newbie

Joined: Wed Dec 27, 2006 10:03 am
Posts: 19
Now it seems clear for me. load() proxies the result object itself (in my case, ModelObject). get() proxies only attributes of result object, not the object itself.

Thanks!


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.