NHibernate提交数据库更改,而无需显式调用以保存或更新 [英] NHibernate commit db changes without explict call to save or update

查看:53
本文介绍了NHibernate提交数据库更改,而无需显式调用以保存或更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET中使用Nhibernate 2.0. 我从页面的开头开始事务,并在最后提交事务. 在页面期间: -我有一个对象 -我更改对象属性 -我验证对象 -如果可以,我可以在该对象上调用保存更新 -如果验证错误,则我不会对该对象进行任何保存更新更新的调用 -我总是在页面末尾提交交易.

I use Nhibernate 2.0 in ASP.NET. I start the transaction at the begging of the page and commit the transaction at the end. During the page: - I get an object - I change the object property - I validate the object - if validation is ok I call save-update on that object - if validation is wrong i don't make any call to save-update on that object - I always commit the transaction at the end of page.

问题在于,当验证错误时,我也没有进行任何调用来保存对象的更新,即事务处理actactin会在数据库中进行更改.

The problem is that also when the validation is wrong and i dont make any call to save-update on the object the commit transactin commit the change in the DB.

我将FlushMode设置为从不但未更改.

I set the FlushMode to Never but nothig change.

有建议吗?我错了吗?

推荐答案

在页面期间:-我得到一个对象

During the page: - I get an object

如果您从会话中获取对象,那么您就误解了Update.更新用于将现有实体附加到会话.如果您从会话中获取实体,则该实体已经附加到该会话中,因此Update没有意义.

If you get an object from a session then you are misunderstanding Update. Update is for attaching an existing entity to a session. If you get an entity from a session it is already attached to that session so Update is meaningless.

在这种情况下,SaveOrUpdate与Update无关紧要-一样.

SaveOrUpdate vs. Update in this case doesn't matter -- same thing.

NHibernate跟踪会话中对象的更改.当您提交事务或刷新会话时,它将检查是否有任何更改(有更改),然后将其提交到数据库.整个要点是,跟踪更改的对象(肮脏)不是您的工作,而是NHibernates.

NHibernate tracks changes to the object in session. When you commit a transaction or flush a session it is going to check for any changes (which there are) and then commit those to the database. The whole point of this is that it isn't your job to track which objects are changed (dirty), it is NHibernates.

其他ORM可能要求您自己跟踪更改,并对要保留的任何对象显式调用某种Update,但是NH不能那样工作.

Other ORM may require that you track the changes yourself and call some kind of Update explicitly on any object changed that you want to persist, but NH doesn't work that way.

所以要回答您的问题,如果验证失败,则您不想提交事务.

So to answer your question, if validation fails you don't want to commit the transaction.

NH也被认为适用于工作单位模式.因此,如果您在与验证工作的业务逻辑不同的逻辑部分中进行提交,则可能会引起冲突.

NH is also opinionated towards the Unit of Work pattern. So if you do the commit in a different logical part of the program from your business logic that is validating work, it probably will cause friction.

这篇关于NHibernate提交数据库更改,而无需显式调用以保存或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆