Search in sources :

Example 16 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class EJBQLQueryMetadata method resolve.

boolean resolve(EntityResolver resolver, EJBQLQuery query) {
    EJBQLCompiledExpression expression = query.getExpression(resolver);
    setPrefetchTree(expression.getPrefetchTree());
    resultSetMapping = expression.getResult() != null ? expression.getResult().getResolvedComponents(resolver) : null;
    ObjEntity root = expression.getRootDescriptor().getEntity();
    if (!super.resolve(root, resolver)) {
        return false;
    }
    if (QueryCacheStrategy.NO_CACHE == getCacheStrategy()) {
        return true;
    }
    // create a unique key based on entity, EJBQL, and parameters
    StringBuilder key = new StringBuilder();
    if (query.getEjbqlStatement() != null) {
        key.append('/').append(query.getEjbqlStatement());
    }
    if (query.getFetchLimit() > 0) {
        key.append('/').append(query.getFetchLimit());
    }
    Map<String, Object> namedParameters = query.getNamedParameters();
    if (!namedParameters.isEmpty()) {
        List<String> keys = new ArrayList<>(namedParameters.keySet());
        Collections.sort(keys);
        for (String parameterKey : keys) {
            key.append('/').append(parameterKey).append('=').append(namedParameters.get(parameterKey));
        }
    }
    Map<Integer, Object> positionalParameters = query.getPositionalParameters();
    if (!positionalParameters.isEmpty()) {
        List<Integer> keys = new ArrayList<>(positionalParameters.keySet());
        Collections.sort(keys);
        for (Integer parameterKey : keys) {
            key.append('/').append(parameterKey).append('=').append(positionalParameters.get(parameterKey));
        }
    }
    if (query.getFetchOffset() > 0 || query.getFetchLimit() > 0) {
        key.append('/');
        if (query.getFetchOffset() > 0) {
            key.append('o').append(query.getFetchOffset());
        }
        if (query.getFetchLimit() > 0) {
            key.append('l').append(query.getFetchLimit());
        }
    }
    this.cacheKey = key.toString();
    return true;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) ArrayList(java.util.ArrayList)

Example 17 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class SQLTemplateMetadata method resolve.

boolean resolve(Object root, EntityResolver resolver, SQLTemplate query) {
    if (super.resolve(root, resolver)) {
        if ((!query.isUseScalar() && !query.isFetchingDataRows()) && (query.getResultColumnsTypes() != null && !query.getResultColumnsTypes().isEmpty())) {
            throw new CayenneRuntimeException("Error caused by using root in query with resultColumnTypes without scalar or dataRow.");
        }
        if (query.getResult() != null && query.getResultColumnsTypes() != null) {
            throw new CayenneRuntimeException("Caused by trying to override result column types of query.");
        }
        if (query.isFetchingDataRows() && query.isUseScalar()) {
            throw new CayenneRuntimeException("Can't set both use scalar and fetching data rows.");
        }
        buildResultSetMappingForColumns(query);
        resultSetMapping = query.getResult() != null ? query.getResult().getResolvedComponents(resolver) : query.isUseScalar() ? new ArrayList<>() : null;
        isSingleResultSetMapping = resultSetMapping != null && resultSetMapping.size() == 1;
        // generate unique cache key...
        if (QueryCacheStrategy.NO_CACHE == getCacheStrategy()) {
        } else {
            // create a unique key based on entity, SQL, and parameters
            StringBuilder key = new StringBuilder();
            ObjEntity entity = getObjEntity();
            if (entity != null) {
                key.append(entity.getName());
            } else if (dbEntity != null) {
                key.append("db:").append(dbEntity.getName());
            }
            if (query.getDefaultTemplate() != null) {
                key.append('/').append(query.getDefaultTemplate());
            }
            Map<String, ?> parameters = query.getParams();
            if (!parameters.isEmpty()) {
                List<String> keys = new ArrayList<>(parameters.keySet());
                Collections.sort(keys);
                for (String parameterKey : keys) {
                    key.append('/').append(parameterKey).append('=').append(parameters.get(parameterKey));
                }
            }
            for (Object parameter : query.getPositionalParams()) {
                key.append("/p:").append(parameter);
            }
            if (query.getFetchOffset() > 0 || query.getFetchLimit() > 0) {
                key.append('/');
                if (query.getFetchOffset() > 0) {
                    key.append('o').append(query.getFetchOffset());
                }
                if (query.getFetchLimit() > 0) {
                    key.append('l').append(query.getFetchLimit());
                }
            }
            this.cacheKey = key.toString();
        }
        return true;
    }
    return false;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ArrayList(java.util.ArrayList)

Example 18 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class SelectById method createReplacementQuery.

@Override
protected Query createReplacementQuery(EntityResolver resolver) {
    ObjEntity entity = root.resolve(resolver);
    ObjectSelect<?> query = new ObjectSelect<>().entityName(entity.getName()).where(idSpec.getQualifier(entity)).cacheStrategy(cacheStrategy, cacheGroup);
    if (prefetches != null) {
        query.prefetch(prefetches);
    }
    if (fetchingDataRows) {
        query.fetchDataRows();
    }
    return query;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity)

Example 19 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class RelationshipQuery method updateMetadata.

void updateMetadata(EntityResolver resolver) {
    // caching metadata as it may be accessed multiple times (at a DC and DD level)
    if (metadataResolver != resolver) {
        if (objectId == null) {
            throw new CayenneRuntimeException("Can't resolve query - objectID is null.");
        }
        ClassDescriptor descriptor = resolver.getClassDescriptor(objectId.getEntityName());
        this.arc = (ArcProperty) descriptor.getProperty(relationshipName);
        if (arc == null) {
            throw new CayenneRuntimeException("No relationship named %s found in entity %s; object id: %s", relationshipName, objectId.getEntityName(), objectId);
        }
        this.metadata = new DefaultQueryMetadata() {

            @Override
            public boolean isRefreshingObjects() {
                return refreshing;
            }

            @Override
            public ObjEntity getObjEntity() {
                return arc.getTargetDescriptor().getEntity();
            }

            @Override
            public ClassDescriptor getClassDescriptor() {
                return arc.getTargetDescriptor();
            }

            @Override
            public int getStatementFetchSize() {
                return statementFetchSize;
            }
        };
        this.metadataResolver = resolver;
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 20 with ObjEntity

use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.

the class NestedDataContextWriteIT method testCommitChangesToParentFlattened.

@Test
public void testCommitChangesToParentFlattened() {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("Master");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("Detail1");
    childO2.addToArtistArray(childO1);
    ObjEntity ent = childContext.getEntityResolver().getObjEntity("ArtGroup");
    Collection<ObjRelationship> rels = ent.getDeclaredRelationships();
    assertEquals(3, rels.size());
    assertEquals(1, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(() -> {
        childContext.commitChangesToParent();
        assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
        assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
        Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
        assertNotNull(parentO1);
        assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
        ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
        assertNotNull(parentO2);
        assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
        assertEquals(1, parentO1.getGroupArray().size());
        assertEquals(1, parentO2.getArtistArray().size());
        assertTrue(parentO2.getArtistArray().contains(parentO1));
        assertTrue(parentO1.getGroupArray().contains(parentO2));
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11