Hibernate 和 NonUniqueObjectException
我有一个实体,其中包含两个具有 @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;
}
如果我尝试保存具有另一个 A 记录的“B_ID”和“C_ID”的 A 实例,我会得到异常:
org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联< /strong>
例如:
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 和 C_ID 在我的模型中不是唯一的,并且 (B_ID + C_ID) 不是唯一约束!!
我能做什么?
提前致谢。
i have an entity that contains two other entities with @ManyToOne relationship.
@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:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
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!!
What can i do?
Thank in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Hibernate 并不是在抱怨数据库的唯一性,而是在抱怨当前的 Session 已经包含一个与您尝试保存的新对象具有相同 ID 的对象。它不允许这样做 - Hibernate 有严格的要求,即给定的 ID 不能在单个会话范围内由两个不同的对象表示。
在某些时候,您的应用程序已保存或加载了具有相同 ID 的实体,并且该对象已在会话中“注册”。在这种特定情况下,很难判断它抱怨的是哪个 ID,因为异常文本不清楚。尝试暂时删除级联指令,看看它是否仍然发生,尝试缩小范围。
如有必要,您可以强制会话“忘记”给定 ID 的任何现有对象(在 Hibernate API 中使用
Session.evict()
或EntityManager.detach()
code> 在 JPA 2.0 API 中),但这不是一个非常优雅的解决方案。重申一下 - 这个异常与数据库约束完全无关,它与 Hibernate 保护其内部内存状态的一致性有关。
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, orEntityManager.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.