没有会话的 NHibernate
我们正在构建一个翻译应用程序,它从一个数据库读取数据并将其翻译成一种完全不同的格式。 我们将使用 NHibernate 从尚不可用的源数据库中读取数据。 我们希望通过虚拟源对象、将其传递到我们的翻译核心并验证输出来测试翻译核心逻辑。 每次我们尝试将项目添加到一对多集合(声明为 PersistentGenericBag)时,我们都会收到 NHibernate.LazyInitializationException:Initializing[Unavailable#] - 无法延迟初始化集合,没有会话或会话被关闭。 有人解决过这个问题吗?
private PersistentGenericBag<Child> _Children = null;
[NHibernate.Mapping.Attributes.Bag(1, Name = "Children", Table = "Children", Lazy = CollectionLazy.False, Cascade = "all")]
[NHibernate.Mapping.Attributes.Key(2)]
[NHibernate.Mapping.Attributes.Column(3, Name = "ParentId")]
[NHibernate.Mapping.Attributes.OneToMany(4, ClassType = typeof(Child))]
public virtual PersistentGenericBag<Child> Children
{
get
{
if (_Children == null)
{
_Children = new PersistentGenericBag<Child>();
}
return _Children;
}
set { _Children = value; }
}
[TestMethod]
public void Xyz()
{
Parent parent = new Parent ();
parent.Children.Add(new Child()); //exception thrown here
Assert.AreEqual(parent.Children.Count, 1);
}
We are building a translation application which reads data from one database and translates it into a completely different format. We will be using NHibernate to read from the source database which is not yet available. We want to test the translation core logic by dummying up a source object, pass it through our translation core, and validating the output. Every time we attempt to add items to a one to many collection (declared as a PersistentGenericBag) we get NHibernate.LazyInitializationException: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed. Has anyone tackled this?
private PersistentGenericBag<Child> _Children = null;
[NHibernate.Mapping.Attributes.Bag(1, Name = "Children", Table = "Children", Lazy = CollectionLazy.False, Cascade = "all")]
[NHibernate.Mapping.Attributes.Key(2)]
[NHibernate.Mapping.Attributes.Column(3, Name = "ParentId")]
[NHibernate.Mapping.Attributes.OneToMany(4, ClassType = typeof(Child))]
public virtual PersistentGenericBag<Child> Children
{
get
{
if (_Children == null)
{
_Children = new PersistentGenericBag<Child>();
}
return _Children;
}
set { _Children = value; }
}
[TestMethod]
public void Xyz()
{
Parent parent = new Parent ();
parent.Children.Add(new Child()); //exception thrown here
Assert.AreEqual(parent.Children.Count, 1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意 mausch 的观点,您应该在域模型中尝试坚持使用标准 .Net 集合元素(例如 IList),而不是 NHibernate 集合。
完成此操作后,您应该能够在集合中添加和删除项目,而不会出现任何 NHibernate 问题。
I agree with mausch, you should use try to stick to standard .Net collection elements like IList in your domain model instead of the NHibernate collections.
Once you do that you should be able to add and remove items from the collection without having any issues with NHibernate.