0

Spring 3、休止状態などを使用しています。テーブルのカウンター列を更新しようとしています。行の選択を行い、行からデータを新しいオブジェクトにコピーしてから、新しいオブジェクトで saveOrUpdate を実行しようとしています次のエラーが表示されます。

私のソースコード:

public void updateSerialNumber(SerialNumber sn) {
        SerialNumber MySN = new SerialNumber();
        log.debug("sn:" + sn.toString());
        MySN.setName(sn.getName());
        MySN.setValue(sn.getValue());
        MySN.setSerialNumberId(sn.getSerialNumberId());
        log.debug("MySN:" + MySN.toString());
        sessionFactory.getCurrentSession().saveOrUpdate(MySN);              
}

私のエラー:

2012-07-25 13:46:30,725 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [org.uftwf.model.SerialNumber#CERT]
    at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
4

1 に答える 1

0

if you only want to update the counter why are you copying the Object? You cannot have two different objects with the same id (@Id in your Entity) assosiated with the hibernate session. I guess you're using one of the properties you are setting on MySN as the ID. I'd suggest using a surrogate ID (and afaik this is als what is recommended in hibernate documentation) which can be e.g. assigned by a db-sequence (if you're using oracle DB) and if you have to copy the object just leave this surrogate id null and you'll be able to save it.

于 2012-07-25T18:05:44.823 に答える