use of org.dbflute.exception.EntityAlreadyUpdatedException in project dbflute-core by dbflute.
the class AbstractBehaviorWritable method helpInsertOrUpdateInternally.
protected <RESULT extends ENTITY> void helpInsertOrUpdateInternally(RESULT entity, InsertOption<CB> insOption, UpdateOption<CB> updOption) {
assertEntityNotNull(entity);
if (helpDetermineInsertOrUpdateDirectInsert(entity)) {
doCreate(entity, insOption);
return;
}
RuntimeException updateException = null;
try {
doModify(entity, updOption);
} catch (EntityAlreadyUpdatedException e) {
// already updated (or means not found)
updateException = e;
} catch (EntityAlreadyDeletedException e) {
// means not found
updateException = e;
} catch (OptimisticLockColumnValueNullException e) {
// means insert?
updateException = e;
}
if (updateException == null) {
return;
}
final CB cb = newConditionBean();
final Set<String> uniqueDrivenProperties = entity.myuniqueDrivenProperties();
if (uniqueDrivenProperties != null && !uniqueDrivenProperties.isEmpty()) {
for (String prop : uniqueDrivenProperties) {
final DBMeta dbmeta = entity.asDBMeta();
final ColumnInfo columnInfo = dbmeta.findColumnInfo(prop);
// already checked in update process
final Object value = columnInfo.read(entity);
cb.localCQ().invokeQueryEqual(columnInfo.getColumnDbName(), value);
}
} else {
cb.acceptPrimaryKeyMap(asDBMeta().extractPrimaryKeyMap(entity));
}
if (readCount(cb) == 0) {
// anyway if not found, insert
doCreate(entity, insOption);
} else {
throw updateException;
}
}
Aggregations