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;
}
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;
}
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;
}
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;
}
}
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));
});
}
Aggregations