nhibernate 映射:删除集合,使用旧 ID 插入新集合

发布于 2024-08-27 02:03:40 字数 925 浏览 5 评论 0原文

我的问题与此类似:(link)< /a>

但我有一对多关联:(

<set name="Fields" cascade="all-delete-orphan" lazy="false" inverse="true">
  <key column="[TEMPLATE_ID]"></key>
  <one-to-many class="MyNamespace.Field, MyLibrary"/>
</set>

我也尝试使用) 此映射适用于 Template 对象。这个对象和 Field 对象的 ID 生成器设置为 identity

所以当我为 Template 对象调用 session.Update 时,它​​工作得很好,几乎: 如果Field对象有Id号,则调用UPDATE sql请求,如果Id为0,则执行INSERT。但是,如果我从集合中删除 Field 对象,它对数据库没有影响。我发现如果我也为这个 Field 对象调用 session.Delete ,一切都会好起来的,但由于客户端-服务器架构,我不知道要删除什么。

所以我决定从数据库中删除所有集合元素并使用新集合调用 session.Update 。我遇到了一个问题:nhibernate 对具有非零 Id 的 Field 对象执行 UPDATE 操作,但它们已从数据库中删除!

也许我应该使用其他一些 ID 生成器或其他东西.. 让 nhibernate 执行集合的“全部删除”/“全部插入”例程的最佳方法是什么?

my issue lokks similar to this one: (link)

but i have one-to-many association:

<set name="Fields" cascade="all-delete-orphan" lazy="false" inverse="true">
  <key column="[TEMPLATE_ID]"></key>
  <one-to-many class="MyNamespace.Field, MyLibrary"/>
</set>

(i also tried to use )
this mapping is for Template object. this one and the Field object has their ID generators set to identity.

so when i call session.Update for the Template object it works fine, well, almost:
if the Field object has an Id number, UPDATE sql request is called, if the Id is 0, the INSERT is performed. But if i delete a Field object from the collection it has no effect for the Database. I found that if i also call session.Delete for this Field object, everything will be ok, but due to client-server architecture i don't know what to delete.

so i decided to delete all the collection elements from the DB and call session.Update with a new collection. and i've got an issue: nhibernate performs the UPDATE operation for the Field objects that has non-zero Id, but they are removed from DB!

maybe i should use some other Id generator or smth..
what is the best way to make nhibernate perform "delete all"/"insert all" routine for the collection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-09-03 02:03:40

您正在更新的实体是否已与会话关联? (即您是否加载实体并修改加载的实例)?

听起来您正试图告诉 nhibernate 更新一个分离的实体,在这种情况下,nhiberante 无法知道在集合中添加/删除了哪些实体。在这种情况下,您可以使用合并:

var mergedEntity = session.Merge(entityPasedFromClient)

合并操作将从数据库中获取实体,将其与从客户端发送的实体进行比较,然后合并它们,这样nhiberante从数据库中获取的实体(并与session)被修改并稍后获取,返回合并的实体(这将不是与您传递合并操作的实体相同的实例)。

我不确定我是否理解你问题的最后一部分:
“所以我决定从数据库中删除所有集合元素并使用新集合调用 session.Update。我遇到了一个问题:nhibernate 对具有非零 Id 的 Field 对象执行 UPDATE 操作,但它们是从数据库中删除!”

字段项目是否更新然后删除?

Is the entity you are updateing already associated with the session? (ie do you load the entity and modify that loaded instance)?

It sound like you are trying to tell nhibernate to update a detached entity, in this case nhiberante cannot know what entities as been added/removed in the collection. In this case you could use Merge:

var mergedEntity = session.Merge(entityPasedFromClient)

The merge operation will fetch the enity from the db compare it with the one that as been sent from the client and merge them, that way the entity that nhiberante fetch from the db (and is associated with the session) is modified and later fetched, the merged entity is returned (this will not be the same instance as the entity you pass the merge operation).

I am not sure I understand the last part of your question:
"so i decided to delete all the collection elements from the DB and call session.Update with a new collection. and i've got an issue: nhibernate performs the UPDATE operation for the Field objects that has non-zero Id, but they are removed from DB!"

Are the field items updated and then removed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文