具有相同标识符值的不同对象已经与保存时的会话错误相关联 [英] a different object with the same identifier value was already associated with the session error on save

查看:26
本文介绍了具有相同标识符值的不同对象已经与保存时的会话错误相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Spring + Hibernate:一个不同的对象相同的标识符值已经与会话相关联

我的休眠注释一直有问题.我有两个类之间的双向关系.这是映射(感谢 axtavt):

I've been having problems with my hibernate annotations. I have a bidirectional relationship between 2 classes. Here's the mapping(thanks to 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);

它产生这个错误:

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)

但是当我把它改成

session.merge(receipt)

它没有错误,但是当我检查我的数据库时,collections 表上的receiptId fk 被设置为空...任何帮助表示赞赏.谢谢^_^...

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 ^_^...

推荐答案

Receipt 上的 mappedby 注释意味着 Collection 实际上是关系的拥有方,这显然不是您想要的,因为您在 Receipt 上有级联.

The mappedby annotation on the Receipt means that the Collection is actually the owning side of the relationship, which is clearly not what you intended since you have a cascade on the Receipt.

您应该删除 Receipt 上的 mappedby 并按照休眠文档中的此示例进行操作:

You should remove the mappedby on the Receipt and follow this example from the hibernate documentation:

@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.

这里有更多信息:http:///docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

这篇关于具有相同标识符值的不同对象已经与保存时的会话错误相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆