Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you call Repository.Save, you are notifying the session held by the repository to track that object and synchronize the changes to the database at the next flush. Until you flush the session, no changes are made to the database. The object does become part of the session cache though, and so will be returned by the Get(1).</p> <p>When you run a query to populate a collection, the session queries the database to get the results, unless it has already cached those results. <strike>Since you have not updated the database yet, the Car you added to the session will not be part of the result set.</strike> (&lt;-- Possibly incorrect) If I'm reading the documentation correctly, both query results and Save()ed entities should be added to the session (first-level) cache. That doesn't necessarily mean that the <code>querystatement.List()</code> queries the cache after adding the DB results... I'm having a hard time wrapping my head around exactly what's going on.</p> <p>As an aside, I believe you can set the session to autoflush but I'd have to check the documentation.</p> <p>UPDATE: </p> <p>I think I might see what's going on here. The default session <code>FlushMode</code> is <code>Auto</code> but Rhino's <code>UnitOfWork.Start()</code> creates a session with a <code>FlushMode</code> set to <code>Commit</code>, which means the session will not auto-flush unless you explicitly call <code>Flush()</code> or commit the transaction. With a <code>FlushMode</code> of <code>Auto</code>, NHibernate will (sometimes?) flush the session before querying to prevent stale data from being returned. If I'm right, your DB transaction looks something like:</p> <pre><code>SELECT * FROM Car INSERT INTO Car (...) VALUES (...) </code></pre> <p>When it Auto flushes seems a bit ambiguous from the documentation/blogs I have read... the most common answer is that it with <code>FlushMode = Auto</code> it flushes "sometimes" although guarantees that <code>Session.Find</code> will never return stale data. Since NHibernate Linq actually just creates a Criteria query, it might not trigger the auto flush (maybe this has been fixed now... it's hard to know).</p> <p>So it seems to me that in your case you want to flush after your save because you immediately want to retrieve the results of your save. In a smaller unit of work where you were only updating entities, a single Commit() would be fine. Maybe <code>UnitOfWork.CurrentSession.FlushMode = FlushMode.Auto;</code> would do the trick but the fact that the UOW Factory explicitly sets the mode to Commit seems to be encouraging you to really think about your UOW boundaries. </p>
    singulars
    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.
    1. VO
      singulars
      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