Search in sources :

Example 11 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DataDomainIndirectDiffBuilder method arcCreated.

@Override
public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
    ObjEntity entity = resolver.getObjEntity(((ObjectId) nodeId).getEntityName());
    ObjRelationship relationship = entity.getRelationship(arcId.toString());
    if (relationship.isSourceIndependentFromTargetChange()) {
        ObjectId nodeObjectId = (ObjectId) nodeId;
        if (!nodeObjectId.isTemporary()) {
            indirectModifications.add(nodeObjectId);
        }
        if (relationship.isFlattened()) {
            if (relationship.isReadOnly()) {
                throw new CayenneRuntimeException("Cannot set the read-only flattened relationship '%s' in ObjEntity '%s'.", relationship.getName(), relationship.getSourceEntity().getName());
            }
            // Register this combination (so we can remove it later if an insert occurs before commit)
            FlattenedArcKey key = new FlattenedArcKey((ObjectId) nodeId, (ObjectId) targetNodeId, relationship);
            // If this combination has already been deleted, simply undelete it.
            if (!flattenedDeletes.remove(key)) {
                flattenedInserts.add(key);
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 12 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DataNode method performQueries.

/**
 * Runs queries using Connection obtained from internal DataSource.
 *
 * @since 1.1
 */
@Override
public void performQueries(Collection<? extends Query> queries, OperationObserver callback) {
    int listSize = queries.size();
    if (listSize == 0) {
        return;
    }
    if (callback.isIteratedResult() && listSize > 1) {
        throw new CayenneRuntimeException("Iterated queries are not allowed in a batch. Batch size: %d", listSize);
    }
    // do this meaningless inexpensive operation to trigger AutoAdapter lazy
    // initialization before opening a connection. Otherwise we may end up
    // with two
    // connections open simultaneously, possibly hitting connection pool
    // upper limit.
    getAdapter().getExtendedTypes();
    Connection connection = null;
    try {
        connection = this.getDataSource().getConnection();
    } catch (Exception globalEx) {
        getJdbcEventLogger().logQueryError(globalEx);
        Transaction transaction = BaseTransaction.getThreadTransaction();
        if (transaction != null) {
            transaction.setRollbackOnly();
        }
        callback.nextGlobalException(globalEx);
        return;
    }
    try {
        DataNodeQueryAction queryRunner = new DataNodeQueryAction(this, callback);
        for (Query nextQuery : queries) {
            // catch exceptions for each individual query
            try {
                queryRunner.runQuery(connection, nextQuery);
            } catch (Exception queryEx) {
                getJdbcEventLogger().logQueryError(queryEx);
                // notify consumer of the exception,
                // stop running further queries
                callback.nextQueryException(nextQuery, queryEx);
                Transaction transaction = BaseTransaction.getThreadTransaction();
                if (transaction != null) {
                    transaction.setRollbackOnly();
                }
                break;
            }
        }
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
        // ignore closing exceptions...
        }
    }
}
Also used : BaseTransaction(org.apache.cayenne.tx.BaseTransaction) Transaction(org.apache.cayenne.tx.Transaction) SelectQuery(org.apache.cayenne.query.SelectQuery) BatchQuery(org.apache.cayenne.query.BatchQuery) Query(org.apache.cayenne.query.Query) SQLException(java.sql.SQLException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Connection(java.sql.Connection) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 13 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DataNodeSyncQualifierDescriptor method reset.

void reset(DbEntityClassDescriptor descriptor) {
    attributes = new ArrayList<>(3);
    valueTransformers = new ArrayList<>(3);
    usingOptimisticLocking = descriptor.getEntity().getLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC;
    // master PK columns
    if (descriptor.isMaster()) {
        for (final DbAttribute attribute : descriptor.getDbEntity().getPrimaryKeys()) {
            attributes.add(attribute);
            valueTransformers.add(input -> {
                ObjectId id = (ObjectId) input.getNodeId();
                return id.getIdSnapshot().get(attribute.getName());
            });
        }
    } else {
        // TODO: andrus 12/23/2007 - only one step relationship is supported...
        if (descriptor.getPathFromMaster().size() != 1) {
            throw new CayenneRuntimeException("Only single step dependent relationships are currently supported. Actual path length: %d", descriptor.getPathFromMaster().size());
        }
        DbRelationship masterDependentDbRel = descriptor.getPathFromMaster().get(0);
        if (masterDependentDbRel != null) {
            for (final DbJoin dbAttrPair : masterDependentDbRel.getJoins()) {
                DbAttribute dbAttribute = dbAttrPair.getTarget();
                if (!attributes.contains(dbAttribute)) {
                    attributes.add(dbAttribute);
                    valueTransformers.add(input -> {
                        ObjectId id = (ObjectId) input.getNodeId();
                        return id.getIdSnapshot().get(dbAttrPair.getSourceName());
                    });
                }
            }
        }
    }
    if (usingOptimisticLocking) {
        for (final ObjAttribute attribute : descriptor.getEntity().getAttributes()) {
            if (attribute.isUsedForLocking()) {
                // only care about first step in a flattened attribute
                DbAttribute dbAttribute = (DbAttribute) attribute.getDbPathIterator().next();
                // only use qualifier if dbEntities match
                if (dbAttribute.getEntity().equals(descriptor.getDbEntity()) && !attributes.contains(dbAttribute)) {
                    attributes.add(dbAttribute);
                    valueTransformers.add(input -> input.getSnapshotValue(attribute.getName()));
                }
            }
        }
        for (final ObjRelationship relationship : descriptor.getEntity().getRelationships()) {
            if (relationship.isUsedForLocking()) {
                // only care about the first DbRelationship
                DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
                for (final DbJoin dbAttrPair : dbRelationship.getJoins()) {
                    DbAttribute dbAttribute = dbAttrPair.getSource();
                    // relationship transformers override attribute transformers for meaningful FK's...
                    // why meaningful FKs can go out of sync is another story (CAY-595)
                    int index = attributes.indexOf(dbAttribute);
                    if (index >= 0 && !dbAttribute.isForeignKey()) {
                        continue;
                    }
                    Function<ObjectDiff, Object> transformer = input -> {
                        ObjectId targetId = input.getArcSnapshotValue(relationship.getName());
                        return targetId != null ? targetId.getIdSnapshot().get(dbAttrPair.getTargetName()) : null;
                    };
                    if (index < 0) {
                        attributes.add(dbAttribute);
                        valueTransformers.add(transformer);
                    } else {
                        valueTransformers.set(index, transformer);
                    }
                }
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) Function(java.util.function.Function) DbAttribute(org.apache.cayenne.map.DbAttribute) ArrayList(java.util.ArrayList) List(java.util.List) ObjectId(org.apache.cayenne.ObjectId) DbJoin(org.apache.cayenne.map.DbJoin) ObjRelationship(org.apache.cayenne.map.ObjRelationship) Map(java.util.Map) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbRelationship(org.apache.cayenne.map.DbRelationship) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 14 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DefaultDataRowStoreFactory method setUpEventBridge.

private void setUpEventBridge(DataRowStore store) {
    if (isNoopEventBridge) {
        return;
    }
    try {
        store.setEventBridge(eventBridge);
        store.startListeners();
    } catch (Exception ex) {
        throw new CayenneRuntimeException("Error initializing DataRowStore.", ex);
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException)

Example 15 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class FlattenedArcKey method lazyJoinSnapshot.

private Map<String, Object> lazyJoinSnapshot() {
    List<DbRelationship> relList = relationship.getDbRelationships();
    if (relList.size() != 2) {
        throw new CayenneRuntimeException("Only single-step flattened relationships are supported in this operation: %s", relationship);
    }
    DbRelationship firstDbRel = relList.get(0);
    DbRelationship secondDbRel = relList.get(1);
    List<DbJoin> fromSourceJoins = firstDbRel.getJoins();
    List<DbJoin> toTargetJoins = secondDbRel.getJoins();
    Map<String, Object> snapshot = new HashMap<>(fromSourceJoins.size() + toTargetJoins.size(), 1);
    for (DbJoin join : fromSourceJoins) {
        Object value = new PropagatedValueFactory(id1.getSourceId(), join.getSourceName());
        snapshot.put(join.getTargetName(), value);
    }
    for (DbJoin join : toTargetJoins) {
        Object value = new PropagatedValueFactory(id2.getSourceId(), join.getTargetName());
        snapshot.put(join.getSourceName(), value);
    }
    return snapshot;
}
Also used : HashMap(java.util.HashMap) DbRelationship(org.apache.cayenne.map.DbRelationship) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbJoin(org.apache.cayenne.map.DbJoin) PropagatedValueFactory(org.apache.cayenne.access.DataDomainSyncBucket.PropagatedValueFactory)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9