NHibernate的映射属性+脏检查属性、NHibernate

2023-09-07 05:00:57 作者:我只在乎在乎我的人

我有问题,NHibernate的更新我的一些实体时,这是不应该发生的(脏检查)。 当我使用NHibernate.Mapping.Attributes地图我的班,我发现有一个参数检查,以NHMA的元素类。我想知道如果我可以通过这个参数设置为false或东西(需要的类型此参数是字符串,所以它可能不是)关闭脏检查。

任何帮助将是AP preciated!

解决方案

首先,这不是你应该做的。 NHibernate的尝试是最好做的东西,如脏检查无误。如果NHibernate的认为这是肮脏的,它可能是。尝试找出你做出引起NHibernate的认为这是肮脏的,看看您是否可以通过调整该解决您的问题,实体有什么变化。

这是说,有一个解决方案。 NHibernate的使用监听器插入和更新前的那团火。更多相关信息可以在http://ayende.com/Blog/archive/2009/04/29/nhibernate-i$p$pupdateeventlistener-amp-i$p$pinserteventlistener.aspx, http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html和其他许多地方的。

事件监听器本身有返回值。这是什么返回值的作用是告诉NHibernate的是否执行真正的插入/更新SQL查询。当您返回,它执行它们。当您返回,事实并非如此。这样你就可以燮preSS实际持久化到数据库。

这种方法的好处是,如果NHibernate的而言,它认为,真正的实体是持续,所以NHibernate的内部状态保持正确,实体变得不脏。

I have problems with NHibernate updating some of my entities when this is not supposed to happen (dirty checking). As I use NHibernate.Mapping.Attributes to map my classes, I have found that there is a parameter "Check" to the element "Class" of NHMA. I would like to know if I can turn off dirty checking by setting this parameter to false or something (the required type for this parameter is string, so it may not be that).

asp增删改查模板 C 数据操作系列 12 NHibernate的增删改查

Any help would be appreciated !

解决方案

Firstly, this is not something you should do. NHibernate tries it very best to do stuff like dirty checking correct. If NHibernate thinks it's dirty, it probably is. Try to find out what changes you've made to the entity which cause NHibernate to think it's dirty and see whether you can solve your issue by tuning this.

That said, there is a solution. NHibernate uses listeners that fire before insert and update. More information on this can be found at http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx, http://www.codinginstinct.com/2008/04/nhibernate-20-events-and-listeners.html and many other locations.

The event listeners themselves have a return value. What this return value does is tell NHibernate whether to execute the actual insert/update SQL queries. When you return false, it executes them. When you return true, it does not. This way you can suppress the actual persistence to the database.

The nice thing of this approach is that where NHibernate is concerned, it believes that the entities actually were persisted, so the internal state of NHibernate stays correct and the entities become not-dirty.