Hibernate and NonUniqueObjectException

拟墨画扇 提交于 2019-11-29 14:17:44

Hibernate is not complaining about database uniqueness here, it's complaining that the current Session already contains an object with the same ID as a new object that you're trying to save. It won't permit this - Hibernate has a strict requirement that a given ID cannot be represented by two different objects in the scope of a single session.

At some point, your application has save dor loaded an entity with that same ID, and that object is already "registered" with the session. It's hard to tell in this specific case which ID it's complaining about, since the exception text isn't clear. Try temporarily removing the cascade directives and see if it still happens, try to it narrow down.

If necessary, you can force the session to "forget" about any existing objects for a given ID (using Session.evict() in the Hibernate API, or EntityManager.detach() in the JPA 2.0 API), but that's not a very elegant solution.

To reiterate - this exception has nothing at all to do with the database constraints, it's about Hibernate protecting the consistency of its internal in-memory state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!