Eclipselink删除和创建导致唯一约束异常
我有一个具有自定义设置主键的实体。 首先,我删除该实体,然后使用相同的主键重新创建该实体。 这会导致提交/刷新时出现独特的约束异常。 作为解决方法,我在删除和创建操作之间调用刷新。 有没有办法正确/更好地完成这些事情,即无需在实体管理器上调用刷新?
谢谢, 曼弗雷德
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 :)