4

重複の可能性:
Spring + Hibernate : 同じ識別子値を持つ別のオブジェクトが既にセッションに関連付けられていた

休止状態の注釈に問題がありました。2 つのクラス間に双方向の関係があります。マッピングは次のとおりです(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)

エラーはありませんが、データベースをチェックすると、コレクションテーブルのrecipientId fkがnullに設定されています...どんな助けも大歓迎です。ありがとうございます^_^...

4

1 に答える 1

5

mappedby注釈は、が実際にはリレーションシップの所有側であるReceiptことを意味します。 CollectionReceipt

の を削除mappedbyReceipt、休止状態のドキュメントから次の例に従う必要があります。

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

于 2010-11-16T17:21:35.607 に答える