如何防止NHibernate为引用的对象创建重复记录

时间:2009-10-08 21:23:54

标签: nhibernate

我在使用NHibernate将对象保存到数据库时遇到了麻烦。

我的代码如下所示:

Image image = new Image { Url = 'test.jpg' };
Product product1 = new Product { Name = 'MyProduct', Image = image };
Product product2 = new Product { Name = 'MyProduct2', Image = image };

MySession.Save(product1); // This also creates an Image record
MySession.Save(product2); // This creates another Image record even tho the data is exactly the same

我想阻止图像被创建两次。 是否有标准的NHibernate解决方案,还是我必须为Product类创建自定义Save()方法?

1 个答案:

答案 0 :(得分:0)

经过长时间的搜索,我找到了一些关于为我的实体类实现Equals()方法的东西,以便NHibernate可以比较它们。 也许这是解决我问题的方法。