Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate stateless session - unable to load, update and save object due to reference property
    primarykey
    data
    text
    <p>I'm working on some code that is doing some moderately heavy data processing with NHibernate (yes, I know, perhaps not really the right tool for the job. But it's what I've got to work with)</p> <p>I use a stateless session to get the first 1000 objects, check, update and add them to a list of updated objects if needed. I then repeat for the next 1000 objects until I'm done. Below is a significantly simplified version of the code.</p> <pre><code>List&lt;MarketOrder&gt; updated = new List&lt;MarketOrder&gt;(); IStatelessSession session = SessionProvider(); int offset = 0; bool moreToGet = true; while (moreToGet) { var result = session.Query&lt;MarketOrder&gt;().Where(o =&gt; o.Owner.ID == currentEntityID &amp;&amp; !o.Updated); var tmp = result.Skip(offset).Take(1000); foreach (MarketOrder item in data) { notUpdated.OrderState = MarketOrderState.Closed; updated.Add(notUpdated); } if (data.Count == pageSize) { offset += pageSize; } else { moreToGet = false; } } </code></pre> <p>Another stateless session is then opened to do a bulk update</p> <pre><code>IStatelessSession session = SessionProvider(); foreach (MarketOrder o in updated) { session.Update(o); } </code></pre> <p>When I run my unit test, I get an error:</p> <pre><code>Test 'TestProcess_AutoCloseOrder' failed: NHibernate.PropertyValueException : Error dehydrating property value for Data.Models.MarketOrder.Owner ---- NHibernate.PropertyAccessException : Exception occurred getter of Common.Models.ModelBase.Version -------- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ------------ NHibernate.SessionException : proxies cannot be fetched by a stateless session </code></pre> <p>After some messing around, I figured that the error is because, in the Update, NHibernate is trying to access the <code>Owner</code> property of the <code>MarketOrder</code> being saved. (I assume this is to cascade the update) <code>Owner</code> is populated with a proxy and since it was loaded from a stateless session, cannot be accessed.</p> <p>Sure enough, if I load from a regular session instead of a stateless session it all works fine. This solution will result in a performance hit in production though so I'd rather avoid it.</p> <p>I could set <code>Owner</code> to not lazy load but that is not a practical solution for all similar cases.</p> <p>The answer to this question: <a href="https://stackoverflow.com/questions/8143609/nhibernate-proxies-cannot-be-fetched-by-a-stateless-session-error-message">NHibernate: proxies cannot be fetched by a stateless session error message</a>. Suggests fetching the problem property as part of the initial load but that seems very wasteful. I only want to update a few properties on the main object, why should I have to load all that object's child references as well?</p> <p>What I want is for NHibernate to realise that only the main <code>MarketOrder</code> object has changed and not bother to cascade the update to <code>Owner</code>. In fact, now I think about it, I didn't think Stateless Session was supposed to cascade updates at all?</p> <p>So anyone know what's going on here and how I can resolve it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload