when I update an object, I get a duplicate key exception, and debug code, I found this stack trace.
DefaultSaveOrUpdateEventListener.performSaveOrUpdate
// RR become DETACHED state
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
EntityState entityState = this.getEntityState(event.getEntity(), event.getEntityName(), event.getEntry(), event.getSession());
switch(entityState) {
case DETACHED:
// go into this method
this.entityIsDetached(event);
return null;
case PERSISTENT:
return this.entityIsPersistent(event);
default:
return this.entityIsTransient(event);
}
}
// method2
protected void performUpdate(SaveOrUpdateEvent event, Object entity, EntityPersister persister) throws HibernateException {
boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled && !persister.isMutable()) {
LOG.trace("Immutable instance passed to performUpdate()");
}
if (traceEnabled) {
LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), event.getSession().getFactory()));
}
EventSource source = event.getSession();
EntityKey key = source.generateEntityKey(event.getRequestedId(), persister);
// here throw exp
source.getPersistenceContext().checkUniqueness(key, entity);
if (this.invokeUpdateLifecycle(entity, persister, source)) {
this.reassociate(event, event.getObject(), event.getRequestedId(), persister);
} else {
(new OnUpdateVisitor(source, event.getRequestedId(), entity)).process(entity, persister);
source.getPersistenceContext().addEntity(entity, persister.isMutable() ? Status.MANAGED : Status.READ_ONLY, (Object[])null, key, persister.getVersion(entity), LockMode.NONE, true, persister, false);
persister.afterReassociate(entity, source);
if (traceEnabled) {
LOG.tracev("Updating {0}", MessageHelper.infoString(persister, event.getRequestedId(), source.getFactory()));
}
this.cascadeOnUpdate(event, persister, entity);
}
}
// method3
StatefulPersistenceContext
public Object getEntity(EntityKey key) {
return this.entitiesByKey.get(key);
}
the object which we want to update is in this entitiesByKey, so we get an exception
can someone tell why hibernate will have this check logic? Thanks in advance.