Eclipselink删除和创建导致唯一约束异常

发布于 2024-11-10 05:18:33 字数 151 浏览 3 评论 0原文

我有一个具有自定义设置主键的实体。 首先,我删除该实体,然后使用相同的主键重新创建该实体。 这会导致提交/刷新时出现独特的约束异常。 作为解决方法,我在删除和创建操作之间调用刷新。 有没有办法正确/更好地完成这些事情,即无需在实体管理器上调用刷新?

谢谢, 曼弗雷德

I have an entity that has a custom set primary key.
First I delete this entity and afterwards I recreate this entity with the same primary key.
This leads to a unique constraint exception on commit/flush.
As a workaround I call a flush between the delete and create operation.
Is there a way to do these things right/better, i.e. without calling the flush on the entitymanager?

Thanks,
Manfred

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

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

发布评论

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

评论(1

歌入人心 2024-11-17 05:18:33

em.remove( yourEntity ) 命令将预订您的实体从持久性上下文中删除,然后从数据库中删除,但它不会立即生效。我可以想象,当您持久化新实体时,还没有从持久化上下文和数据库中删除。

您可以尝试在remove()命令后使用自定义JPQL查询来删除此实体,例如“DELETE FROM YourEntity ye WHERE ye.primary1 = :primary1 AND ye.primary2 = :primary2...whatever;” 。我认为这会立即从持久性上下文中删除您的实体,但我不确定。尝试一下,然后告诉我们:)

em.remove( yourEntity ) command will book your entity for removal from the Persistence Context and afterwards from the database, but it does not take effect immediatelly. I can imagine that removal from persistence context and database had not already happened when you persisted the new entity.

You may try to delete this entity with a custom JPQL query like "DELETE FROM YourEntity ye WHERE ye.primary1 = :primary1 AND ye.primary2 = :primary2... whatever;" after remove() command. I assume this removes your entity from the persistence context immediatelly, but I'm not sure. Give it a try, and let us know :)

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