NHibernate 映射到“非规范化”桌子
我有以下对象模型(基于我无法更改的旧表结构)。
public class Store
Contact BillingContact {get;set;}
Contact SetupContact {get;set;}
ISet<Contact> Contacts {get;set;}
在这种情况下,当我创建新商店时,我还会将新联系人添加到联系人列表中。我还想将该联系人设置为 BillingContact 和 SetupContact 上的引用。然后将该批次保存为单个交易。如果我保存商店而不设置对 BillingContact 和 SetupContact 的引用,则一切都很好。但是当我设置这些属性时,NHibernate 尝试在插入后执行更新以更新引用,并且我收到错误,因为存储实际上尚未插入。
看起来我可能必须使用触发器或存储过程,但我希望有一种 NHibernate 方式。
正在运行的 SQL 是
INSERT INTO CONTACTS (Id, CompanyId, Name, etc.) Values (1234, NULL, "My Contact", etc.) NOTE: 1234 was retrieved from a sequence
then
INSERT INTO company (Id, BillingContactId, SetupContactId) Values (8946, 1234, 1234)
then - 错误是因为这个原因
UPDATE CONTACTS SET CompanyId=8946 WHERE CUSTOMER_ID = 1234 AND TimeStamp = xxx
,因为插入尚未提交,所以最终语句失败。
错误是 行已被另一个事务更新或删除(或未保存值映射不正确)[Customer#1234]
I have the following object model (based on a legacy table structure that I can't change).
public class Store
Contact BillingContact {get;set;}
Contact SetupContact {get;set;}
ISet<Contact> Contacts {get;set;}
In this scenario, when I create a new Store, I also add a new contact to the Contacts list. I also want to set that contact to be the reference on the BillingContact and the SetupContact. Then save the lot as a single transaction. If I save the store without setting the Reference to BillingContact and SetupContact, everything is fine. But when I set those properties, NHibernate tries to do an Update after the Insert to update the references and I get an error because the Store hasn't actually been inserted yet.
It looks like I may have to use a trigger or stored proc, but I'm hoping there is an NHibernate way.
The SQL being run is
INSERT INTO CONTACTS (Id, CompanyId, Name, etc.) Values (1234, NULL, "My Contact", etc.) NOTE: 1234 was retrieved from a sequence
Then
INSERT INTO company (Id, BillingContactId, SetupContactId) Values (8946, 1234, 1234)
Then - Error is because of this
UPDATE CONTACTS SET CompanyId=8946 WHERE CUSTOMER_ID = 1234 AND TimeStamp = xxx
Since the insert has not been committed the final statement fails.
The error is
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) [Customer#1234]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为这些引用配置级联以保存更新。这意味着,当您保存或更新实体时,它将“通过级联”保存引用。
http://wiki. Fluentnhibernate.org/Getting_started
在映射部分有一个级联的例子。
You need to configure the cascade for those references to Save-Update. This means, when you save or update an entity, it will save the references "by cascade".
http://wiki.fluentnhibernate.org/Getting_started
In the Mappings section there's an example with cascade.