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.  [ 7 posts ] 
Author Message
 Post subject: Load vs. Get in 1.2.0.Beta1
PostPosted: Mon Sep 25, 2006 4:21 pm 
Newbie

Joined: Tue Jul 18, 2006 10:12 am
Posts: 6
Consider the following mapped classes:

public abstract class Content {}

public class Image : Content {}

Now, when trying to load an instance of an Image:

[...]
Guid imageId = GetId();
ISession sess = GetSession();
object loadedObj = sess.Load(typeof(Content), imageId);
object getObj = sess.Get(typeof(Content), imageId);
[...]

loadedObj.GetType().BaseType will be "Content" (unexpected).
getObj.GetType().BaseType will be "Image" (expected).

Why the difference between Get and Load? Is this a bug, or should I use ISession.Get to suit my needs?

With 1.0.1, ISession.Load would have returned an Image instance above.

Thanks!

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:17 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
out of curiosity, what does loadedObj.GetType() return? and getObj.GetType() ? are these returning proxy classes?

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:20 pm 
Newbie

Joined: Tue Jul 18, 2006 10:12 am
Posts: 6
Both methods seem to return proxy classes. However, it seems like loadedObj.GetType() does not extend Image; but getObj.GetType() does.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 6:46 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
My understanding is that Load will create a proxy but will not actually hit the DB until a property is accessed. Get, however, will immediately load the object into the proxy.

If a proxy for a superclass is loaded using Load() it's not possible to know what subclass it represents until the database is hit, therefore using GetType() won't yeild the expected result.

You're better off using NHibernate.NHibernateUtil.GetClass(proxy) to find out the actual object type that a proxy represents.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 9:51 am 
Newbie

Joined: Tue Jul 18, 2006 10:12 am
Posts: 6
Ah, thanks for the explanation of the subtle differences :) It hasn't been a big deal since I've changed our code to use ISession.Get(), so I may stick with that.

Thanks again,

Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 11:25 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
Good to know, thanks for clearing that up. rduffield, make sure you give him credit for answering your question ;)

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 6:27 pm 
Newbie

Joined: Tue Jul 18, 2006 10:12 am
Posts: 6
Not sure who else I've given credit to.

At any rate, I've also abstracted away NHibernate.NHibernateUtil.GetClass into our helper class for future use. This also works as expected.

Thanks,

Ryan


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