Search in sources :

Example 1 with EntityAlreadyUpdatedException

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;
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) EntityAlreadyUpdatedException(org.dbflute.exception.EntityAlreadyUpdatedException) EntityAlreadyDeletedException(org.dbflute.exception.EntityAlreadyDeletedException) OptimisticLockColumnValueNullException(org.dbflute.exception.OptimisticLockColumnValueNullException)

Aggregations

DBMeta (org.dbflute.dbmeta.DBMeta)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 EntityAlreadyDeletedException (org.dbflute.exception.EntityAlreadyDeletedException)1 EntityAlreadyUpdatedException (org.dbflute.exception.EntityAlreadyUpdatedException)1 OptimisticLockColumnValueNullException (org.dbflute.exception.OptimisticLockColumnValueNullException)1