NHibernate - 孤儿未删除 - 外键设置为空
我通过 REST Web 服务从 Javascript 发送 JSON 集合,使用 Json.NET 进行反序列化,然后最终使用 NHibernate 在数据库中进行更新(我使用的是 Fluent)。
我的 Json 是:
{
"ID": 1,
"Name": "ObjectName",
"Keys": [
{
"ID": 6,
"Name": "ID"
}
]
}
我的困难是,当我从 Javascript 中的“Key”集合中删除一个子项时,后续更新只会清空该子项的外键 - 它实际上并没有删除它。我已经在父级和子级上设置了我认为正确的映射:
Object 一对多映射:
// one-to-many
HasMany(x => x.Keys)
.KeyColumn("ObjectID")
.Cascade.AllDeleteOrphan();
Key 多对一映射:
// many-to-one
References(x => x.Object)
.Cascade.None();
NHibernate 代码执行更新:
using (var transaction = Session.BeginTransaction())
{
Session.SaveOrUpdate(entity);
transaction.Commit();
}
Session.Flush();
虽然这个例子是双向的,但我也尝试过单向映射,但到目前为止还没有效果;尽管关联本身已损坏(FK 设置为 null),但子记录仍保留在数据库中。
我缺少什么明显的东西吗?
I'm sending a JSON collection from Javascript through a REST web service to be Deserialized using Json.NET and then finally updated in the DB using NHibernate (I'm using Fluent).
My Json is:
{
"ID": 1,
"Name": "ObjectName",
"Keys": [
{
"ID": 6,
"Name": "ID"
}
]
}
My difficulty is that when I remove a child from the 'Key' collection in Javascript, the subsequent update only nulls the foreign key of the child - it doesn't actually delete it. I have setup what I think is the correct mappings on both the parent and child:
Object one-to-many mapping:
// one-to-many
HasMany(x => x.Keys)
.KeyColumn("ObjectID")
.Cascade.AllDeleteOrphan();
Key many-to-one mapping:
// many-to-one
References(x => x.Object)
.Cascade.None();
NHibernate code that performs the update:
using (var transaction = Session.BeginTransaction())
{
Session.SaveOrUpdate(entity);
transaction.Commit();
}
Session.Flush();
Although this example is bidirectional, I have also attempted uni-directional mappings, but so far this has had no effect; the child record persists in the database, although the association itself is broken (FK set to null).
Anything obvious that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这里的问题在于,以正常能力使用 Json.NET 反序列化器本质上会创建一个新对象 - 然后该对象由 NHibernate 持久化。最终结果是数据库中的记录被保留,新子对象集合中存在的所有子对象也被保留 - 但是被删除的子对象是孤立的 - 因为没有使用 .Remove 或 直接从持久化的 NHibernate 对象中删除。 。清除。
解决方案分为两部分。首先,我们必须使用 Json.NET 的 CustomCreationConverter 来传入要处理(合并)的现有对象的实例。
然而,仅此方法是行不通的,因为现有的对象子集合将由 json 集合中的子集合添加。为了解决这个问题,并确保 NHibernate 知道在接收到结果对象时要做什么,我们需要做一些 Json.NET hack。
Json.Net >连载> JsonSerializerInternalReader.cs
重新编译并重新添加 DLL 后(它有效),在 javascript 中删除的子项最终也会从数据库中删除。
Ok, the problem here lies in the fact that using the Json.NET Deserializer in a normal capacity essentially creates a new object - this object is then persisted by NHibernate. The end result is the record in the database is kept, as are all the child objects that exist in the new child object collection - however children that were removed are orphaned - since the where not removed directly from the persisted NHibernate object using .Remove or .Clear.
The solution is in two parts. Firstly, we have to use the
CustomCreationConverter
of Json.NET to pass in an instance of the existing object to be worked on (merge).This alone will not work however, as the exiting objects child collections will be added to by the children in the json collection. The remedy this, and to ensure that NHibernate knows what to do when receiving the resulting object, we need to do a bit of a Json.NET hack.
Json.Net > Serialization > JsonSerializerInternalReader.cs
After re-compiling, and re-adding the DLL - it works - children that are removed in javascript are finally removed from the DB as well.