具有相同标识符值的不同对象已与保存时的会话错误相关联

发布于 2024-10-03 02:54:20 字数 2536 浏览 9 评论 0原文

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

我的休眠注释一直存在问题。我在两个类之间有双向关系。这是映射(感谢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; 
    ...
}

但是当我尝试使用以下方式保存带有集合列表的收据时:

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)

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

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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-10-10 02:54:20

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

您应该删除 Receipt 上的 mappedby 并按照 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; 
    ...
}

使用与上面相同的代码来执行保存应该可行。

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

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.

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.

There is some more information on this here: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文