Sorry, I've just realised the article by Ayende doesn't accurately explain (though my sample code does) what the problem is.
A better description is found here, in this StackOverflow thread: http://stackoverflow.com/questions/1286827/nhibernate-save-get-problem On Tue, Apr 10, 2012 at 3:27 PM, John Thornborrow < [email protected]> wrote: > Hi, > > We are using Automatic flush mode, but it is very explicitly and precisely > not flushing when we query. The code example I posted in my previous post > is factually correct, the second line (i.e. query) will/does throw an > exception. > > Cannot viably use Get()/Load() because the Query is composed further up > the chain, thus the Law of Demeter kicks in and well.. it's pretty ugly for > the domain to know what is and isn't a concern of the DB anyway. :) > > Though I must ask.. why implement a Cache if it is not going to be > Queryable? > > Regards, > John. > > On Tue, Apr 10, 2012 at 2:40 PM, Oskar Berggren > <[email protected]>wrote: > >> >> > However, as I understand from a now quite old article[1] on Ayende's >> >> > blog that this is "not possible" unless we explicity use >> Get()/Load(). >> >> I understand the point of Ayende's article to mainly concern >> performance. If you know the primary key, the fastest way is >> Get()/Load(), and you should use them unless there is a good reason >> not to. >> >> Using a query method will bypass the cache and always execute in the >> database. However, using automatic flush mode, NH can automatically >> insert the row for the newly saved object when the same table is hit >> by the query. >> >> /Oskar >> >> >> >> Den 10 april 2012 15:18 skrev John Thornborrow < >> [email protected]>: >> > Hi, >> > >> > The question is simply: Does the restriction as documented in Ayende's >> > article in 2009 still exist today? >> > >> > I appreciate Querying will hit the DB - but this is a problem >> introduced by >> > NH and its Cache (because it will not insert immediately when >> > ISession.Save() is called.) >> > >> > Over-Simplified code to demonstrate point: >> > >> > public void Foo(ISession session) { >> > session.Save(new Client { Id = "123" }); >> > var client = session.Query<Client>().Single(each => each.Id == >> "123"); // >> > InvalidOpException - Sequence does not contain any elements >> > } >> > >> > Regards, >> > John. >> > >> > On Tue, Apr 10, 2012 at 1:31 PM, Oskar Berggren < >> [email protected]> >> > wrote: >> >> >> >> Not sure if I understand what the question is, but NH is not a query >> >> execution engine - querying will hit the database. >> >> >> >> /Oskar >> >> >> >> >> >> Den 10 april 2012 12:49 skrev John T <[email protected]>: >> >> > Hi guys, >> >> > >> >> > I've hit a problem where we have a case that we are creating an >> >> > entity, saving it, then Query<T>()ing for it from cache and modifying >> >> > it. I.e. not flushing in between the initial Save() and Query(). >> >> > >> >> > However, as I understand from a now quite old article[1] on Ayende's >> >> > blog that this is "not possible" unless we explicity use >> Get()/Load(). >> >> > >> >> > Has the situation improved since then, or is the same restriction >> >> > still in place? >> >> > >> >> > Many thanks, >> >> > John. >> >> > >> >> > [1] >> >> > >> http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id >> >> > >> >> > -- >> >> > You received this message because you are subscribed to the Google >> >> > Groups "nhusers" group. >> >> > To post to this group, send email to [email protected]. >> >> > To unsubscribe from this group, send email to >> >> > [email protected]. >> >> > For more options, visit this group at >> >> > http://groups.google.com/group/nhusers?hl=en. >> >> > >> >> >> >> -- >> >> You received this message because you are subscribed to the Google >> Groups >> >> "nhusers" group. >> >> To post to this group, send email to [email protected]. >> >> To unsubscribe from this group, send email to >> >> [email protected]. >> >> For more options, visit this group at >> >> http://groups.google.com/group/nhusers?hl=en. >> >> >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups >> > "nhusers" group. >> > To post to this group, send email to [email protected]. >> > To unsubscribe from this group, send email to >> > [email protected]. >> > For more options, visit this group at >> > http://groups.google.com/group/nhusers?hl=en. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "nhusers" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/nhusers?hl=en. >> >> > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
