I am using Spring 3, hibernate etc. I am trying to update a counter col in my table.. I do a select of the row, copy the data from the row to a new object and then try to do a saveOrUpdate with the new object and I get the following error:
My source code:
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);
}
My Error:
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)
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
MySNas 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 idnulland you’ll be able to save it.