spring 具有相同标识符值的不同对象已经与保存时的会话错误相关联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4194350/
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
a different object with the same identifier value was already associated with the session error on save
提问by mileesah
Possible Duplicate:
Spring + Hibernate : a different object with the same identifier value was already associated with the session
I've been having problems with my hibernate annotations. I have a bidirectional relationship between 2 classes. Here's the mapping(thanks to axtavt):
我的休眠注释一直有问题。我有两个类之间的双向关系。这是映射(感谢axtavt):
@Entity
public class Receipt implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "receipt")
private List<Collection> collections;
...
}
@Entity
public class Collection implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="ReceiptId")
private Receipt receipt;
...
}
But when i try to save my receipt with a list of collections using:
但是,当我尝试使用以下收藏品列表保存我的收据时:
Receipt r = new Receipt();
List<Collection> cols = new ArrayList<Collection>();
cols.add(new Collection());
r.setCollections(cols);
getHibernateTemplate().save(r);
It generates this error:
它生成此错误:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.coa.acctreports.pojo.Collection#0]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.coa.acctreports.pojo.Collection#0]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at com.coa.acctreports.daoImp.AccountingReportsImpl.save(AccountingReportsImpl.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
but when i change it to
但是当我把它改成
session.merge(receipt)
it has no errors but when i check my database the receiptId fk on the colllections table is set to null... Any help is appreciated. Thanks ^_^...
它没有错误,但是当我检查我的数据库时,collections 表上的receiptId fk 被设置为空...任何帮助表示赞赏。谢谢^_^...
回答by StevenWilkins
The mappedbyannotation on the Receiptmeans that the Collectionis actually the owning side of the relationship, which is clearly not what you intended since you have a cascade on the Receipt.
上的mappedby注释Receipt意味着Collection实际上是关系的拥有方,这显然不是您想要的,因为您在Receipt.
You should remove the mappedbyon the Receiptand follow this example from the hibernate documentation:
您应该删除mappedby的Receipt,并按照从Hibernate文档这个例子:
@Entity
public class Receipt implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="ReceiptId")
private List<Collection> collections;
...
}
@Entity
public class Collection implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="ReceiptId",insertable=false,updatable=false)
private Receipt receipt;
...
}
Using the same code you have above to perform the save should work.
使用与上面相同的代码来执行保存应该可以工作。
There is some more information on this here: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
这里有更多信息:http: //docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

