0
votes

I want to keep an entity, being configured by the user on several pages, in Session. This entity is loaded with NHibernate, with some of its properties/collections lazy-loaded. Say:

  • Session["order"] = new Order(productRepository.Get(id))
  • on some next page, get Session["order"] and now work with it

but, at this time order is OK but its Product (and nested stuff) is broken since they're lazy-loaded in different session.

Is it possible to tell NHibernate that I want to eager-load my transient order's properties to the deepest level? Or, the only solution will be to eager-load at the time of productRepository.Get(id) ? Like, Session.LoadNestedProperties(order, Eager);

Update: http://www.ribbing.net/index.php?option=com_content&task=view&id=35&Itemid=1 seems to solve the issue. However I'm not sure that reflection is great...

1

1 Answers

0
votes

You could eager load all the graph with the objects you need, which is a little bit tricky.

Or you could try the following:

I assume your Order has a single Product. This Product is your problem since it becomes a detached object when the user visits the second page. You could use something like:

session.Update(myorder.Product)

to reattach the Product instance to the current session. After that lazy loading should work fine.