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

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

问题描述

我在 ASP.NET 中使用 Nhibernate 2.0.我在页面请求时开始事务并在最后提交事务.在页面期间:- 我得到一个对象- 我改变了对象属性- 我验证对象- 如果验证没问题,我会对该对象调用 save-update- 如果验证错误,我不会对该对象进行任何保存更新调用- 我总是在页面末尾提交事务.

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.

问题是,当验证错误并且我没有对对象进行任何调用 save-update 时,commit transactin 会在数据库中提交更改.

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 设置为 Never 但没有任何变化.

I set the FlushMode to Never but nothig change.

有建议吗?我错了什么?

Have suggestion? What I mistake?

推荐答案

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

During the page: - I get an object

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

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 可能要求您自己跟踪更改,并对要保留的任何已更改对象显式调用某种更新,但 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天全站免登陆