NHibernate 更新导致选择行的计算列
我有一个想要更新的 User 类。我的数据库中有一个用户表,其中有两列:名字和姓氏,还有一个名为 DisplayName 的计算列,它将两列连接起来,中间有一个空格。 DisplayName 的 NHibernate 映射是
<property name="DisplayName" type="string" generated="always"/>
当我更新 User 对象并提交事务时,NHibernate 仅在 DisplayName 属性上运行额外的 select 语句,我假设保持对象和数据库行同步。我不需要它,因为对象随后就超出了范围。
我可以做些什么来告诉 NHibernate 此时不需要获取更新的 DisplayName 吗?
问候, F。
I've got a User class that I want to update. There's a User table in my database with two columns among others: FirstName and Lastname and there's a computed column called DisplayName that concatenates the two with a space in the middle. The NHibernate mapping for DisplayName is
<property name="DisplayName" type="string" generated="always"/>
When I update the User object and commit the transaction, NHibernate runs an extra select statement just on the DisplayName property, I presume to keep the object and the DB row in sync. I don't need that as the object goes out of scope right afterwards.
Is there something I can do to tell NHibernate that there's no need to get the updated DisplayName at this time?
Regards,
F.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
generated="always"
的确切含义是:“这是每次我修改此行时数据库生成的值;请为我刷新它”。NH 没有“仅在特定列发生更改时才刷新”之类的概念。
我的观点是,您应该在域模型而不是数据库中生成该值。
generated="always"
means exactly that: "this is a value generated by the DB every time I modify this row; please refresh it for me".NH does not have a concept like "only refresh this if a particular column changed".
My opinion is that you should generate that value in your domain model instead of the DB.
将
generate="always"
替换为insert="false" update="false"
。它将禁用对此列的插入和更新,并且不会执行额外的 select 语句来刷新该值。Replace
generated="always"
withinsert="false" update="false"
. It will disable inserts and updates to this column and will not make an extra select statement to refresh the value.