a different object with the same identifier value was already associated with the session

spring mvc + jpa(hibernate) 出现如下错误:


a different object with the same identifier value was already associated with the session因为之前已经将该条记录取到了session缓存中,update时发生错误,一个游离态对象和一个持久态对象,具有相同OID,因此报错。


网上找了下,都不大对,其实是因为关联出错,这个问题一般是manytoone onetoone onetomany 这种关系时候发生的,加上cascade级联就好了


比如我的user:

 @OneToOne(fetch=FetchType.EAGER,mappedBy="baseUser",cascade=CascadeType.ALL)
    private UserInfo info;

UserInfo

@OneToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL) 
private User baseUser;

你可能感兴趣的:(a different object with the same identifier value was already associated with the session)