是否可以禁止在NHibernate中插入后进行更新? [英] Is it possible to suppress the Update after Insert in NHibernate?

查看:56
本文介绍了是否可以禁止在NHibernate中插入后进行更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被用来研究使用NHibernate的新应用程序的数据库性能问题.我注意到,在某些表NH上插入了一行,然后用完全相同的数据更新了同一行.到目前为止,我所收集的是在对已定义多对一关系的表进行插入后进行的更新.

I've been thrown into researching a database performance issue with a new application using NHibernate. I noticed that on some tables NH does an insert of a row followed by an update of the same row with exactly the same data. What I have gathered so far is that updates after inserts are done on tables that have many-to-one relationships defined.

在任何情况下,我都希望NH认为数据是相同的,并且会抑制额外的更新.有没有办法完全抑制这种额外的更新?

In any case, I would have expected that NH figures that the data is the same and suppresses the extra update. Is there a way to suppress this extra update at all?

推荐答案

听起来您在多对一关联上具有双向关系.

It sounds like you have a bidirectional relationship on a many-to-one association.

该问题的解决方案是在关系的一对多端添加inverse="true". 这里是一个例子.

The solution to that problem is to add inverse="true" on the one-to-many side of the relationship. Here is an example.

插入操作由保存数据的孩子执行.通过父级设置数据库中子级记录的parentId来执行更新.如果关系是单向的,则第一个插入将不具有parentId,并且将需要更新.对于双向关系,正如您所描述的,此更新是多余的. inverse="true"告诉父母,孩子负责维持这种关系,从而防止了额外的更新.

The insert is performed by the child saving its data. The update is performed by the parent setting the parentId of the child record in the database. If the the relationship was uni-directional, the first insert would not have the parentId and the update would be required. With a bi-directional relationship, the update is superfluous as you describe. The inverse="true" tells the parent that the child is responsible for maintaining the relationship thus preventing the extra update.

这篇关于是否可以禁止在NHibernate中插入后进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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