﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Suiden,
  
HQL or Criteria are the things to use
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment25</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment25</guid><pubDate>Wed, 03 Jun 2009 10:49:03 GMT</pubDate></item><item><title>Suiden commented on NHibernate – The difference between Get, Load and querying by id</title><description>Which of the 3 ways is best to load an Author by Id AND his blogs (mapped one-to-many, lazy)?
  
  
Note: author.Blogs is lazy because 95% these are not needed on-the-fly; this scenario is about the other 5% when blogs must be available without making another round-trip to database..
  
  
In general: how to best load an entity by Id and a series of associations at the same time?
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment24</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment24</guid><pubDate>Wed, 03 Jun 2009 07:04:08 GMT</pubDate></item><item><title>Ryan Heath commented on NHibernate – The difference between Get, Load and querying by id</title><description>Aaah, I somehow had the impression Get never checked the cache, but upon rereading your post this sentence "Get will usually result in a select against the database, but it will check the session cache and the 2nd level cache first to get the values first." makes me happy again :)
  
  
// Ryan
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment23</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment23</guid><pubDate>Wed, 06 May 2009 10:48:15 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Ryan,
  
You use a Get
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment22</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment22</guid><pubDate>Wed, 06 May 2009 10:14:55 GMT</pubDate></item><item><title>Ryan Heath commented on NHibernate – The difference between Get, Load and querying by id</title><description>Perhaps this will show I do not use NH (yet!), but how do you handle a case where you are 95% sure the object is in the database (ie a blogpost) and it better be loaded from the cache instead from the database (it will not change frequently). How to handle the 5% that is not in the database (deleted, wrong id through url hacking, google finds an old url, etc etc)? It feels wrong you have to catch the specific Load exception instead of checking null in order to return a nice 404 instead of an aweful 502 ...
  
  
// Ryan
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment21</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment21</guid><pubDate>Wed, 06 May 2009 09:58:57 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Jon,
  
You should use Get() if you don't know that the entity exists.
  
Because Load will _always_ return a value, Get will return null if the value does not exists
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment20</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment20</guid><pubDate>Mon, 04 May 2009 02:07:42 GMT</pubDate></item><item><title>Jon Kruger commented on NHibernate – The difference between Get, Load and querying by id</title><description>Would it be bad to always use Load() and never use Get()?  Or is there some scenario where you should use Get() over Load()?  It sounds like Load() lets NHibernate figure out how to best handle the loading.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment19</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment19</guid><pubDate>Mon, 04 May 2009 00:00:53 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Neil,
  
Yes, if it is already there, it might cause that.
  
Another nasty side effect that can happen is if there is cascade defined on the object, which might cause NH to initialize all columns to null / default because of this trick
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment18</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment18</guid><pubDate>Fri, 01 May 2009 06:30:24 GMT</pubDate></item><item><title>Neil Mosafi commented on NHibernate – The difference between Get, Load and querying by id</title><description>Makes sense, but only if the object has already been loaded into the session I presume?  I have seen it on some projects I worked on and always thought it looked a bit smelly!  Having read your post I can see that calling Load is definitely the way to do it.
  
  
Cheers
  
Neil
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment17</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment17</guid><pubDate>Thu, 30 Apr 2009 22:32:50 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Anthony,
  
That is why we have FK for
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment16</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment16</guid><pubDate>Thu, 30 Apr 2009 17:53:11 GMT</pubDate></item><item><title>Anthony Dewhirst commented on NHibernate – The difference between Get, Load and querying by id</title><description>I know that you said that "if you know that the value exist in the database" but, thinking about concurrency, what if in your call it did exist but another user has deleted it before you make your call, will NHibernate still check with a select before insert or trust you and hope that you have used a FK constraint in the DB otherwise?
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment15</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment15</guid><pubDate>Thu, 30 Apr 2009 17:51:23 GMT</pubDate></item><item><title>Rob commented on NHibernate – The difference between Get, Load and querying by id</title><description>That's what I thought.  Just wanted to make sure that I wasn't missing something.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment14</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment14</guid><pubDate>Thu, 30 Apr 2009 17:13:42 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Rob,
  
It doesn't apply. If you need custom fetching, you need to use a query.
  
If you consider the reasons for Get / Load, you would see that it make sense that a custom fetching strategy would require a query.
  
There is no way for Get or Load to handle that.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment13</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment13</guid><pubDate>Thu, 30 Apr 2009 17:07:45 GMT</pubDate></item><item><title>Rob commented on NHibernate – The difference between Get, Load and querying by id</title><description>Very enlightening.  How does this all relate to custom fetching strategies?  I can't see a way to apply them using anything but a criteria/query.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment12</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment12</guid><pubDate>Thu, 30 Apr 2009 17:01:54 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Neil,
  
Yuck, that is likely to cause "an object with the same id but with different reference is already associated with the current session"
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment11</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment11</guid><pubDate>Thu, 30 Apr 2009 16:26:47 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Will,
  
I haven't even _considered_ that, but of course, you are right.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment10</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment10</guid><pubDate>Thu, 30 Apr 2009 16:23:34 GMT</pubDate></item><item><title>Neil Mosafi commented on NHibernate – The difference between Get, Load and querying by id</title><description>I have seen this before:
  
  
s.Save(
  
	new Order
  
	{
  
		Amount = amount,
  
		customer = new Customer { Id = 1 }
  
	}
  
);
  
  
I think it works, but I assume that's not recommended?
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment9</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment9</guid><pubDate>Thu, 30 Apr 2009 15:36:02 GMT</pubDate></item><item><title>Will Shaver commented on NHibernate – The difference between Get, Load and querying by id</title><description>Another difference - 
  
  
Query based selects such as the first example will include all defined and active filters on the entity. 
  
  
Load / Get will IGNORE all filters for that entity. Filters set up on the entity's relationships will still be used when loading sets/references on the entity returned from Load / Get. 
  
  
This can cause quite a bit of headache if you're making heavy use of filters and not expecting this behavior.
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment8</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment8</guid><pubDate>Thu, 30 Apr 2009 14:03:33 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Valeriu,
  
Get works based on your mapping, it does't do any eager loading outside of what is defined there
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment7</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment7</guid><pubDate>Thu, 30 Apr 2009 10:10:55 GMT</pubDate></item><item><title>Valeriu commented on NHibernate – The difference between Get, Load and querying by id</title><description>Get will bring back an initialized entity and will eager load all associations? 
  
Or loading associations will depend on your explicit mappings?
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment6</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment6</guid><pubDate>Thu, 30 Apr 2009 10:09:27 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>Andres,
  
Not currently, but that is a good suggestion.
  
  
Ng,
  
That depends on a lot of things, mostly if you have cascade associations.
  
  
  
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment5</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment5</guid><pubDate>Thu, 30 Apr 2009 09:07:10 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate – The difference between Get, Load and querying by id</title><description>MF,
  
Yes, it should be
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment4</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment4</guid><pubDate>Thu, 30 Apr 2009 09:06:07 GMT</pubDate></item><item><title>NG commented on NHibernate – The difference between Get, Load and querying by id</title><description>So, 
  
  
Session.Delete(Session.Load
&lt;customer(1))
  
  
Only goes to the DB once? Cool. I thought it went twice, which always bugged me.
&gt;</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment3</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment3</guid><pubDate>Thu, 30 Apr 2009 07:45:06 GMT</pubDate></item><item><title>Anders commented on NHibernate – The difference between Get, Load and querying by id</title><description>Would this be catched by NHProf?
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment2</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment2</guid><pubDate>Thu, 30 Apr 2009 07:34:07 GMT</pubDate></item><item><title>MF commented on NHibernate – The difference between Get, Load and querying by id</title><description>should the where clause in the first snippet
  
("where customer.Id = customerId")
  
really be:
  
where customer.Id == customerId
  
</description><link>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment1</link><guid>http://ayende.com/3988/nhibernate-the-difference-between-get-load-and-querying-by-id#comment1</guid><pubDate>Thu, 30 Apr 2009 07:14:27 GMT</pubDate></item></channel></rss>