博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Model change control with NHibernate
阅读量:3977 次
发布时间:2019-05-24

本文共 2905 字,大约阅读时间需要 9 分钟。

What ISession.Flush() generally does

ISession.Flush() updates OO side changes to the framework but not to the database; this has at least the following effects

1. Internal flags change so that IsDirty() is always set to false

2. updates internal flags/states of the framework so that it's consistent with the OO model

Internal state related to IsDirty() changes as the OO side changes as NHibernate has wrappers and some other unknown constructs that work closely with entities and keep track of their changes, so although it's still far from enough for NHibernate to be confirmed of changes and get the necessary detailed information of the changes, (and that's why methods like ISession.Delete(),  ISession.Save() are needed) it at least is able to know of the existence of the changes whenever changes are made.

Advantages of Flush

By using ISession.Flush(), you can be sure that all changes are made done to the persistence framework but not to the underlying database, so you can at any time choose to either commit the changes to Database or totally cancel all the changes made since last checkpoint by doing a NHibernate rollback (note: for rolling back, the OO side needs to be restored to the point where the transaction started accordingly)

Confusion of IsDirty() 

A natural misconception about IsDirty() is that it can be alone used as an indicator of whether the model has been changed. Unfortunately if used improperly it might be totally against this use.

From the discussion above, we can see that IsDirty() is changed to true (indicating a change) whenever OO model is changed without flush; in this sense, this method (and its underlying flags) might be sensitive enough to detect changes (need further proof and verification), but if it is used in a untimely manner with flush which may flip its value and may be used anywhere proper for update purpose mentioned above, the program will get a result totally contrary to what it's supposed to be. So the best approach might be using it in a combination with another set of internal constructs kept in your application in accordance to your own business logic to form a correct answer. And also be even more careful when using with advanced application features such as change management and undo/redo capabilities.

Reattachment Problem

If an entity is removed from the OO model it used to be part of as well as detached from the persistence by being called on with ISession.Delete(); and immediately afterwards it is added back in the same model (at the same place or not), and invoked with ISession.Save(), an issue may come up. The primary reason is NH framework was not properly updated upon the deletion before it could accept an addition of the same object. The solution is either do a commit or a flush before the adding operation. And the selection between these two is depending on whether you want it to be updated to the database.

转载地址:http://xheki.baihongyu.com/

你可能感兴趣的文章
python解决导出excel文件时中文文件名乱码
查看>>
Django操作NOSQL(MongoDB)数据库
查看>>
Failed to load JavaHL Library
查看>>
linux学习方法
查看>>
linux中nohup命令的用法
查看>>
vim代码智能提示功能及相关配置
查看>>
linux常用命令——ps
查看>>
linux常用命令——lsof
查看>>
nginx安装手册
查看>>
Nginx配置文件详细说明
查看>>
Nginx负载均衡
查看>>
CMD常用命令
查看>>
JavaScript之回调函数
查看>>
编程中同步/异步;阻塞/非阻塞
查看>>
第一个Java程序
查看>>
conda创建python环境
查看>>
pytorch学习入门:什么是pytorch+安装
查看>>
机器学习中ground truth的解释
查看>>
使用朴素贝叶斯进行分本分类
查看>>
Python读写文件的方式
查看>>