NHibernate - 无法延迟初始化角色集合
我有以下看似简单的场景,但我对 NHibernate 仍然很陌生。
当尝试为我的控制器上的编辑操作加载以下模型时:
控制器的编辑操作:
public ActionResult Edit(Guid id)
{
return View(_repository.GetById(id));
}
存储库:
public SomeModel GetById(Guid id)
{
using (ISession session = NHibernateSessionManager.Instance.GetSession())
return session.Get<SomeModel >(id);
}
模型:
public class SomeModel
{
public virtual string Content { get; set; }
public virtual IList<SomeOtherModel> SomeOtherModel { get; set; }
}
我收到以下错误:
-无法延迟初始化角色集合:SomeOtherModel,没有会话或会话已关闭
我是什么这里不见了?
I have the following seemingly simple scenario, however I'm still pretty new to NHibernate.
When trying to load the following model for an Edit action on my Controller:
Controller's Edit Action:
public ActionResult Edit(Guid id)
{
return View(_repository.GetById(id));
}
Repository:
public SomeModel GetById(Guid id)
{
using (ISession session = NHibernateSessionManager.Instance.GetSession())
return session.Get<SomeModel >(id);
}
Model:
public class SomeModel
{
public virtual string Content { get; set; }
public virtual IList<SomeOtherModel> SomeOtherModel { get; set; }
}
I get the following error:
-failed to lazily initialize a collection of role: SomeOtherModel, no session or session was closed
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您在模型
GetById
方法中创建并关闭了会话。 (using 语句关闭会话)会话在整个业务事务期间必须可用。有多种方法可以实现这一目标。您可以将 NHibernate 配置为使用会话工厂 GetCurrentSession 方法。请参阅nhibernate.info 上的内容或这篇关于代码项目的文章。
我不用这个。我编写了自己的事务服务,它允许执行以下操作:
无论您如何实现它,会话和事务都应该与业务事务(或系统功能)一样长。除非你不能依赖事务隔离也不能回滚整个事情。
The problem is that you create and also close the session in you models
GetById
method. (the using statement closes the session) The session must be available during the whole business transaction.There are several ways to achieve this. You can configure NHibernate to use the session factories GetCurrentSession method. See this on nhibernate.info or this post on Code Project.
I don't use this. I wrote my own transaction service which allows the following:
However you implement it, sessions and transactions should live as long as a business transaction (or system function). Unless you can't rely on transaction isolation nor rollback the whole thing.
如果您打算在关闭会话之前使用它,则需要立即加载
SomeOtherModel
集合:默认情况下 FluentNHibernate 对集合映射使用延迟加载。另一种选择是修改映射中的默认行为:
请注意,如果执行此操作,每次加载可能不想要的父实体时,
SomeOtherModel
都会立即加载(使用外部联接) 。一般来说,我更喜欢始终在映射级别保留默认的延迟加载,并根据情况调整我的查询。You need to eagerly load the
SomeOtherModel
collection if you intend to use it before closing the session:By default FluentNHibernate uses lazy loading for collection mappings. Another option is to modify this default behavior in your mapping:
Note that if you do this
SomeOtherModel
will be eagerly loaded (using an outer join) every time you load the parent entity which might not be want you want. In general I prefer to always leave the default lazy loading at the mapping level and tune my queries depending on the situation.参考: http://nhibernate.info/doc/howto/various /lazy-loading-eager-loading.html
Reference: http://nhibernate.info/doc/howto/various/lazy-loading-eager-loading.html