小编mil*_*sah的帖子

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

可能的重复:
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; 
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用以下收藏品列表保存我的收据时:

Receipt r = new Receipt();
List<Collection> cols = new ArrayList<Collection>();
cols.add(new Collection());
r.setCollections(cols);
getHibernateTemplate().save(r);
Run Code Online (Sandbox Code Playgroud)

它生成此错误:

org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: …
Run Code Online (Sandbox Code Playgroud)

spring annotations hibernate hibernate-annotations

4
推荐指数
1
解决办法
4万
查看次数

在保存期间,hibernate @onetomany关系更新而不是插入

请帮我解决这个问题.我尝试了很多组合,但似乎没有任何效果.我正在尝试使用注释实现hibernate映射,但在保存我的父对象及其子对象期间,我注意到正在调用更新语句而不是insert语句.

我有两个课程,彼此之间有一对多的关系.

这些是类的映射:Receipt具有一对多的集合

@Entity
public class Receipt implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @OneToMany
    @JoinColumn(name="ReceiptId")
    private List<Collection> collections;

    //setters, getters
}


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

    //setters getters
}
Run Code Online (Sandbox Code Playgroud)

问题是在保存收据时如下:

Receipt r = new Receipt();
List<Collection> cols = new ArrayList<Collection>();
cols.add(new Collection());
r.setCollections(cols);
getHibernateTemplate().save(r);
Run Code Online (Sandbox Code Playgroud)

它会生成此错误:

Hibernate: insert into Receipt (userId, dateCreated, payor, receiptDate, receiptNumber, total) values (?, ?, ?, ?, ?, ?)
Hibernate: update Collection …
Run Code Online (Sandbox Code Playgroud)

spring annotations hibernate hibernate-annotations

1
推荐指数
1
解决办法
4543
查看次数