Search in sources :

Example 21 with CayenneRuntimeException

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

the class PrefetchProcessorJointNode method buildPKIndex.

/**
 * Creates an internal index of PK columns in the result.
 */
private void buildPKIndex() {
    // index PK
    Collection<DbAttribute> pks = getResolver().getEntity().getDbEntity().getPrimaryKeys();
    this.idIndices = new int[pks.size()];
    // this is needed for checking that a valid index is made
    Arrays.fill(idIndices, -1);
    Iterator<DbAttribute> it = pks.iterator();
    for (int i = 0; i < idIndices.length; i++) {
        DbAttribute pk = it.next();
        for (int j = 0; j < columns.length; j++) {
            if (pk.getName().equals(columns[j].getName())) {
                idIndices[i] = j;
                break;
            }
        }
        // sanity check
        if (idIndices[i] == -1) {
            throw new CayenneRuntimeException("PK column is not part of result row: %s", pk.getName());
        }
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 22 with CayenneRuntimeException

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

the class PrefetchProcessorTreeBuilder method addNode.

boolean addNode(PrefetchProcessorNode node) {
    List rows;
    ArcProperty arc;
    ClassDescriptor descriptor;
    PrefetchProcessorNode currentNode = getParent();
    if (currentNode != null) {
        rows = (List) extraResultsByPath.get(node.getPath());
        arc = (ArcProperty) currentNode.getResolver().getDescriptor().getProperty(node.getName());
        if (arc == null) {
            throw new CayenneRuntimeException("No relationship with name '%s' found in entity '%s'", node.getName(), currentNode.getResolver().getEntity().getName());
        }
        descriptor = arc.getTargetDescriptor();
    } else {
        arc = null;
        if (this.descriptor != null) {
            descriptor = this.descriptor;
        } else {
            descriptor = queryMetadata.getClassDescriptor();
        }
        rows = mainResultRows;
    }
    node.setDataRows(rows);
    node.setIncoming(arc);
    if (node.getParent() != null && !node.isJointPrefetch()) {
        node.setResolver(new HierarchicalObjectResolverNode(node, context, descriptor, queryMetadata.isRefreshingObjects(), seen));
    } else {
        node.setResolver(new PrefetchObjectResolver(context, descriptor, queryMetadata.isRefreshingObjects(), seen));
    }
    if (node.getParent() == null || node.getParent().isPhantom()) {
        node.setParentAttachmentStrategy(new NoopParentAttachmentStrategy());
    } else if (node.isJointPrefetch()) {
        node.setParentAttachmentStrategy(new StackLookupParentAttachmentStrategy(node));
    } else if (node.getIncoming().getRelationship().isSourceIndependentFromTargetChange()) {
        node.setParentAttachmentStrategy(new JoinedIdParentAttachementStrategy(context.getGraphManager(), node));
    } else {
        node.setParentAttachmentStrategy(new ResultScanParentAttachmentStrategy(node));
    }
    if (currentNode != null) {
        currentNode.addChild(node);
    }
    node.afterInit();
    // push node on stack
    if (nodeStack.isEmpty()) {
        root = node;
    }
    nodeStack.addLast(node);
    return true;
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) List(java.util.List) LinkedList(java.util.LinkedList)

Example 23 with CayenneRuntimeException

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

the class ToOneFault method doResolveFault.

Object doResolveFault(Persistent sourceObject, String relationshipName) {
    RelationshipQuery query = new RelationshipQuery(sourceObject.getObjectId(), relationshipName, false);
    List objects = sourceObject.getObjectContext().performQuery(query);
    if (objects.isEmpty()) {
        return null;
    } else if (objects.size() == 1) {
        return objects.get(0);
    } else {
        throw new CayenneRuntimeException("Error resolving to-one fault. " + "More than one object found. Source Id: %s, relationship: %s", sourceObject.getObjectId(), relationshipName);
    }
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) List(java.util.List)

Example 24 with CayenneRuntimeException

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

the class CreateIfNoSchemaStrategy method generate.

private void generate(DataNode dataNode) {
    Collection<DataMap> map = dataNode.getDataMaps();
    Iterator<DataMap> iterator = map.iterator();
    while (iterator.hasNext()) {
        DbGenerator gen = new DbGenerator(dataNode.getAdapter(), iterator.next(), dataNode.getJdbcEventLogger());
        gen.setShouldCreateTables(true);
        gen.setShouldDropTables(false);
        gen.setShouldCreateFKConstraints(true);
        gen.setShouldCreatePKSupport(true);
        gen.setShouldDropPKSupport(false);
        try {
            gen.runGenerator(dataNode.getDataSource());
        } catch (Exception e) {
            throw new CayenneRuntimeException(e);
        }
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbGenerator(org.apache.cayenne.access.DbGenerator) SQLException(java.sql.SQLException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 25 with CayenneRuntimeException

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

the class EntityRowReader method readRow.

@Override
public DataRow readRow(ResultSet resultSet) {
    try {
        DataRow row = new DataRow(mapCapacity);
        int len = converters.length;
        for (int i = 0; i < len; i++) {
            // note: jdbc column indexes start from 1, not 0 as in arrays
            Object val = converters[i].materializeObject(resultSet, startIndex + i + 1, types[i]);
            row.put(labels[i], val);
        }
        postprocessRow(resultSet, row);
        return row;
    } catch (CayenneRuntimeException cex) {
        // rethrow unmodified
        throw cex;
    } catch (Exception otherex) {
        throw new CayenneRuntimeException("Exception materializing id column.", Util.unwindException(otherex));
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataRow(org.apache.cayenne.DataRow) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

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