java Hibernate 和 NonUniqueObjectException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3543716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate and NonUniqueObjectException
提问by blow
i have an entity that contains two other entities with @ManyToOne relationship.
我有一个实体,其中包含两个其他具有 @ManyToOne 关系的实体。
@Entity
public class A extends Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private B b;
@ManyToOne
@Cascade(CascadeType.SAVE_UPDATE)
private C c;
}
If i try to save an A instance that have "B_ID" and "C_ID" of another A record i get the exception:
如果我尝试保存具有另一条 A 记录的“B_ID”和“C_ID”的 A 实例,则会出现异常:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已经与会话相关联
For example:
例如:
A table
| ID | B_ID | C_ID |
| 1 | 1 | null | // this works
| 2 | null | 1 | // this works
| 3 | 1 | x | // this throws the exception
| 4 | x | 1 | // this throws the exception
x=any value of existent B/C_ID
B_ID and C_ID are not unique in my model and (B_ID + C_ID) is not an unique constraint!!
B_ID 和 C_ID 在我的模型中不是唯一的,并且 (B_ID + C_ID) 不是唯一的约束!!
What can i do?
我能做什么?
Thank in advance.
预先感谢。
回答by skaffman
Hibernate is not complaining about database uniqueness here, it's complaining that the current Sessionalready 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.
Hibernate 不是在这里抱怨数据库的唯一性,而是抱怨当前Session已经包含一个与您要保存的新对象具有相同 ID 的对象。它不允许这样做——Hibernate 有一个严格的要求,即一个给定的 ID 不能由单个会话范围内的两个不同对象表示。
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.
在某些时候,您的应用程序已经保存或加载了具有相同 ID 的实体,并且该对象已经“注册”到会话中。在这种特定情况下,很难判断它抱怨的是哪个 ID,因为异常文本不清楚。尝试暂时删除级联指令,看看它是否仍然发生,尝试缩小范围。
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.
如有必要,您可以强制会话“忘记”给定 ID 的任何现有对象(Session.evict()在 Hibernate API 或EntityManager.detach()JPA 2.0 API 中使用),但这不是一个非常优雅的解决方案。
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.
重申一下 - 这个异常与数据库约束完全没有关系,它是关于 Hibernate 保护其内部内存状态的一致性。

