开发者

Eclipselink delete and create leads to unique constraint exception

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 t开发者_如何学JAVAo 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


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜