If anyone is interested, I got this to work. Setting the class Model to lazy fixed it. I believe it works because the parent that is built when the child is instantiated is a proxy and therefore the grand-parent is not built. From what I have seen, there is no way to have a truly lazy BelongsTo association. The FetchWhen.OnInvoke does not work as I would expect.
In my particular application the stateless session is about 3 times faster when walking very large trees (50,000 nodes). -Dan On Jul 16, 3:41 pm, Dan Jasek <[email protected]> wrote: > I've made it one step further. > It looks like this issue is with the Parent property. NHibernate > stateless sessions do not seem to work with non-lazy loaded/non-join > fetched items. I would much rather it ignore those associations > rather than cause an exception, but whatever. > > If I join fetch the parent it works. For one level deep. Anything > deeper, and the parent attempts to load the grand-parent and fails. > new DetachedQuery("from Model m inner join fetch m.Parent par where > m.Parent = :p").SetParameter("p", current) > > So, I need to set the Parent BelongsTo association be lazy loaded. > But I already though I had done that. My property looks like this: > [BelongsTo(Lazy=FetchWhen.OnInvoke)] > public Model Parent { get; set; } > > The other side is lazy as well, but that seems to work: > [HasMany(Lazy=true)] > public IList<Model> Children { get; set; } > > Why does this still load the entire chain of parents for each query? > How do I set this to lazy load properly? > > Thanks. > > On Jul 14, 10:15 pm, Dan Jasek <[email protected]> wrote: > > > > > I have a tree structure in a database I am accessing through AR. My > > goal is a function to walk the tree. I want to put this in a > > stateless session because the tree can be very large and I don't want > > to spend the resources on the first level cache if it is not > > necissary. > > But I can't seem to get the list of children to work properly in the > > stateless session. > > > This is what I have so far: > > > void WalkTree(Model root, Action<Model> work) { > > using (new StatelessSessionScope()) { > > rWalkTree(root, work); > > } > > > } > > > void rWalkTree(Model current, Action<Model> work) { > > work(current); > > foreach(Model child in Model.FindAllByProperty("Parent", current)) > > { > > rWalkTree(child); > > } > > > } > > > This works fine the first time through the foreach, but errors out > > when run on the first child. I assume it worked the first time > > because current was root which is instantiated outside of the > > stateless session scope. > > > When it errors, it throws the an exception with the inner exception is > > "could not execute query..." and it's inner exception is "possible non- > > threadsafe access to the session", which seems odd (it is all on one > > thread). > > > I have tried a Detached Query and a SQL Query as well. Both of which > > causes the same error, the first time through the foreach. > > > All of these methods work fine on a standard session scope. > > Am I doing something wrong, or is there a better way to do this?- Hide > > quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Castle Project Users" 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/castle-project-users?hl=en.
