Search in sources :

Example 1 with BatchEntityAlreadyUpdatedException

use of org.dbflute.exception.BatchEntityAlreadyUpdatedException in project dbflute-core by dbflute.

the class TnAbstractBatchHandler method handleBatchUpdateResultWithOptimisticLockByResult.

protected void handleBatchUpdateResultWithOptimisticLockByResult(List<?> list, int[] result) {
    if (list.isEmpty()) {
        // for safety
        return;
    }
    final int[] updatedCountArray = result;
    final int entityCount = list.size();
    int index = 0;
    boolean alreadyUpdated = false;
    for (int oneUpdateCount : updatedCountArray) {
        if (entityCount <= index) {
            // for safety
            break;
        }
        if (oneUpdateCount == 0) {
            alreadyUpdated = true;
            break;
        } else if (oneUpdateCount > 1) {
            String msg = "The entity updated two or more records in batch update:";
            msg = msg + " entity=" + list.get(index);
            msg = msg + " updatedCount=" + oneUpdateCount;
            msg = msg + " allEntities=" + list;
            throw new EntityDuplicatedException(msg);
        }
        ++index;
    }
    if (alreadyUpdated) {
        int updateCount = 0;
        for (int oneUpdateCount : updatedCountArray) {
            updateCount = updateCount + oneUpdateCount;
        }
        if (_optimisticLockHandling) {
            throw new BatchEntityAlreadyUpdatedException(list.get(index), 0, updateCount);
        } else {
            String msg = "The entity was NOT found! (might be deleted?):";
            msg = msg + " entity=" + list.get(index);
            msg = msg + " updateCount=" + updateCount;
            msg = msg + " allEntities=" + list;
            throw new EntityAlreadyDeletedException(msg);
        }
    }
}
Also used : BatchEntityAlreadyUpdatedException(org.dbflute.exception.BatchEntityAlreadyUpdatedException) EntityDuplicatedException(org.dbflute.exception.EntityDuplicatedException) EntityAlreadyDeletedException(org.dbflute.exception.EntityAlreadyDeletedException)

Aggregations

BatchEntityAlreadyUpdatedException (org.dbflute.exception.BatchEntityAlreadyUpdatedException)1 EntityAlreadyDeletedException (org.dbflute.exception.EntityAlreadyDeletedException)1 EntityDuplicatedException (org.dbflute.exception.EntityDuplicatedException)1