public TreatAsExpression(Class castClass, ObjectExpression baseExpression) { super(); this.name = "Treat as "+castClass; this.typeExpressionBase = baseExpression; if (baseExpression.isExpressionBuilder()){ this.baseExpression = baseExpression; shouldQueryToManyRelationship = false; hasQueryKey = false; hasMapping = false; } else { this.baseExpression = baseExpression.getBaseExpression(); } shouldUseOuterJoin = true;//this uses outerjoins to the cast class' tables by default. this.castClass = castClass; }
public TreatAsExpression(Class castClass, ObjectExpression baseExpression) { super(); this.name = "Treat as "+castClass; this.typeExpressionBase = baseExpression; if (baseExpression.isExpressionBuilder()){ this.baseExpression = baseExpression; shouldQueryToManyRelationship = false; hasQueryKey = false; hasMapping = false; } else { this.baseExpression = baseExpression.getBaseExpression(); } shouldUseOuterJoin = true;//this uses outerjoins to the cast class' tables by default. this.castClass = castClass; }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); return context.getBaseExpression().twist(selectionCriteria, base); }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); return context.getBaseExpression().twist(selectionCriteria, base); }
/** * INTERNAL: * Parses an expression to return the first non-AggregateObjectMapping expression after the base ExpressionBuilder. * This is used by joining and batch fetch to get the list of mappings that really need to be processed (non-aggregates). * @param aggregateMappingsEncountered - collection of aggregateObjectMapping expressions encountered in the returned expression * between the first expression and the ExpressionBuilder * @return first non-AggregateObjectMapping expression after the base ExpressionBuilder from the fullExpression */ public ObjectExpression getFirstNonAggregateExpressionAfterExpressionBuilder(List aggregateMappingsEncountered) { boolean done = false; ObjectExpression baseExpression = this; ObjectExpression prevExpression = this; while (!baseExpression.getBaseExpression().isExpressionBuilder() && !done) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); while (!baseExpression.isExpressionBuilder() && baseExpression.getMapping().isAggregateObjectMapping()) { aggregateMappingsEncountered.add(baseExpression.getMapping()); baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } if (baseExpression.isExpressionBuilder()) { done = true; //use the one closest to the expression builder that wasn't an aggregate baseExpression = prevExpression; } else { prevExpression = baseExpression; } } return baseExpression; }
@Override public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); if (additionalJoinCriteria != null) { selectionCriteria = selectionCriteria.and(additionalJoinCriteria); } return context.getBaseExpression().twist(selectionCriteria, base); }
/** * INTERNAL: * Returns the nested query corresponding to the expression. * The passed expression should be either join mapping or joined attribute expression. */ public ObjectLevelReadQuery getNestedJoinedMappingQuery(Expression expression) { // the first element of the list is the passed expression, // next one is its base, ... // the last one's base is ExpressionBuilder. ObjectExpression currentExpression = (ObjectExpression)expression; ArrayList<Expression> expressionBaseList = new ArrayList(); do { //skip aggregates since they do not have nested query objects added to JoinedMappingQueries, instead //reference mappings on aggregates are added to the parent's joinAttributeManager if (!currentExpression.getMapping().isAggregateObjectMapping()){ expressionBaseList.add(currentExpression); } currentExpression = (ObjectExpression)currentExpression.getBaseExpression(); } while(!currentExpression.isExpressionBuilder()); // the last expression in the list is not nested - its mapping should have corresponding nestedQuery. DatabaseMapping currentMapping = ((QueryKeyExpression)expressionBaseList.get(expressionBaseList.size() - 1)).getMapping(); ObjectLevelReadQuery nestedQuery = getJoinedMappingQueries_().get(currentMapping); // unless the passed expression was not nested, repeat moving up the list. // the last step is the passed expression (first on the list) getting nested query corresponding to its mapping. for(int i = expressionBaseList.size() - 2; i >= 0; i--) { currentMapping = ((QueryKeyExpression)expressionBaseList.get(i)).getMapping(); nestedQuery = nestedQuery.getJoinedAttributeManager().getJoinedMappingQueries_().get(currentMapping); } return nestedQuery; }
/** * INTERNAL: * Used in case outer joins should be printed in FROM clause. * Each of the additional tables mapped to expressions that joins it. */ public Map additionalExpressionCriteriaMap() { if (getDescriptor() == null) { return null; } HashMap tablesJoinExpressions = null; if(isUsingOuterJoinForMultitableInheritance()) { tablesJoinExpressions = new HashMap(); List childrenTables = getDescriptor().getInheritancePolicy().getChildrenTables(); for( int i=0; i < childrenTables.size(); i++) { DatabaseTable table = (DatabaseTable)childrenTables.get(i); Expression joinExpression = (Expression)getDescriptor().getInheritancePolicy().getChildrenTablesJoinExpressions().get(table); if (getBaseExpression() != null){ joinExpression = getBaseExpression().twist(joinExpression, this); } else { joinExpression = twist(joinExpression, this); } tablesJoinExpressions.put(table, joinExpression); } } return tablesJoinExpressions; }
/** * INTERNAL: * Used in case outer joins should be printed in FROM clause. * Each of the additional tables mapped to expressions that joins it. */ public Map additionalExpressionCriteriaMap() { if (getDescriptor() == null) { return null; } HashMap tablesJoinExpressions = null; if(isUsingOuterJoinForMultitableInheritance()) { tablesJoinExpressions = new HashMap(); List childrenTables = getDescriptor().getInheritancePolicy().getChildrenTables(); for( int i=0; i < childrenTables.size(); i++) { DatabaseTable table = (DatabaseTable)childrenTables.get(i); Expression joinExpression = getDescriptor().getInheritancePolicy().getChildrenTablesJoinExpressions().get(table); if (getBaseExpression() != null){ joinExpression = getBaseExpression().twist(joinExpression, this); } else { joinExpression = twist(joinExpression, this); } tablesJoinExpressions.put(table, joinExpression); } } return tablesJoinExpressions; }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ @Override public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); Expression keySelectionCriteria = this.containerPolicy.getKeySelectionCriteria(); if (keySelectionCriteria != null) { selectionCriteria = selectionCriteria.and(keySelectionCriteria); } return context.getBaseExpression().twist(selectionCriteria, base); }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ @Override public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); Expression keySelectionCriteria = this.containerPolicy.getKeySelectionCriteria(); if (keySelectionCriteria != null) { selectionCriteria = selectionCriteria.and(keySelectionCriteria); } return context.getBaseExpression().twist(selectionCriteria, base); }
while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); if (objectExpression.getBaseExpression().isExpressionBuilder()) { DatabaseMapping mapping = objectExpression.getMapping(); if ((mapping != null) && mapping.isForeignReferenceMapping()) {
throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression()); throw QueryException.castMustUseInheritance(getBaseExpression()); throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression());
/** * INTERNAL: * Compute the cache batched attributes. * Used to recompute batched attributes for nested aggregate queries. */ public void computeBatchReadAttributes() { List<Expression> batchReadAttributeExpressions = getBatchReadAttributeExpressions(); this.batchFetchPolicy.setAttributes(new ArrayList(batchReadAttributeExpressions.size())); int size = batchReadAttributeExpressions.size(); for (int index = 0; index < size; index++) { ObjectExpression objectExpression = (ObjectExpression)batchReadAttributeExpressions.get(index); // Expression may not have been initialized. ExpressionBuilder builder = objectExpression.getBuilder(); if (builder.getSession() == null) { builder.setSession(getSession().getRootSession(null)); } if (builder.getQueryClass() == null) { builder.setQueryClass(getReferenceClass()); } // PERF: Cache join attribute names. ObjectExpression baseExpression = objectExpression; while (!baseExpression.getBaseExpression().isExpressionBuilder()) { baseExpression = (ObjectExpression)baseExpression.getBaseExpression(); } this.batchFetchPolicy.getAttributes().add(baseExpression.getName()); } }
@Override public Expression getJoinCriteria(ObjectExpression context, Expression base) { Expression selectionCriteria = getSelectionCriteria(); Expression keySelectionCriteria = this.containerPolicy.getKeySelectionCriteria(); if (keySelectionCriteria != null) { selectionCriteria = selectionCriteria.and(keySelectionCriteria); } if (additionalJoinCriteria != null) { selectionCriteria = selectionCriteria.and(additionalJoinCriteria); } return context.getBaseExpression().twist(selectionCriteria, base); }
/** * INTERNAL * Return true if it uses a cast class and query is downcasting. It will * look into inheritance hierarchy of the root descriptor. */ public boolean isDowncast(ClassDescriptor rootDescriptor, AbstractSession session) { if (castClass == null){ return false; } if (rootDescriptor.getJavaClass() == castClass){ return false; } ClassDescriptor castDescriptor = session.getClassDescriptor(castClass); if (castDescriptor == null){ throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression()); } if (castDescriptor.getInheritancePolicy() == null){ throw QueryException.castMustUseInheritance(getBaseExpression()); } ClassDescriptor parentDescriptor = castDescriptor.getInheritancePolicy().getParentDescriptor(); while (parentDescriptor != null){ if (parentDescriptor == rootDescriptor){ return true; } parentDescriptor = parentDescriptor.getInheritancePolicy().getParentDescriptor(); } throw QueryException.couldNotFindCastDescriptor(castClass, getBaseExpression()); }
/** * INTERNAL: * Helper method to get the value from the clone for the expression passed in, triggering joins on * all intermediate steps. * Example expression "emp.project.pk" with a clone Employee will trigger indirection and return * the project pk value. * @param session * @param clone * @param expression * @return */ public Object getValueFromObjectForExpression(AbstractSession session, Object clone, ObjectExpression expression){ if (!expression.isExpressionBuilder()){ //can only operate over querykeys representing aggregate Objects. Indirection should not be needed Object baseValue = this.getValueFromObjectForExpression(session, clone, (ObjectExpression)expression.getBaseExpression()); if ( baseValue == null ) { return null; } DatabaseMapping mapping = expression.getMapping(); Object attributeValue = mapping.getRealAttributeValueFromObject(baseValue, session); if (attributeValue != null) { if (mapping.isForeignReferenceMapping() && (((ForeignReferenceMapping)mapping).getIndirectionPolicy().usesTransparentIndirection())) { //getRealAttributeValueFromObject does not trigger transparent indirection, but instantiateObject will (it calls size on it) ((ForeignReferenceMapping)mapping).getIndirectionPolicy().instantiateObject(baseValue, attributeValue); } } return attributeValue; } return clone; }
/** * INTERNAL: * Return the expression to join the main table of this node to any auxiliary tables. */ public Expression additionalExpressionCriteria() { if (getDescriptor() == null) { return null; } Expression criteria = getDescriptor().getQueryManager().getAdditionalJoinExpression(); if(getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { if(isUsingOuterJoinForMultitableInheritance()) { Expression childrenCriteria = getDescriptor().getInheritancePolicy().getChildrenJoinExpression(); childrenCriteria = getBaseExpression().twist(childrenCriteria, this); childrenCriteria.convertToUseOuterJoin(); if(criteria == null) { criteria = childrenCriteria; } else { criteria = criteria.and(childrenCriteria); } } } return criteria; }
/** * INTERNAL: * Return the expression to join the main table of this node to any auxiliary tables. */ public Expression additionalExpressionCriteria() { if (getDescriptor() == null) { return null; } Expression criteria = getDescriptor().getQueryManager().getAdditionalJoinExpression(); if(getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { if(isUsingOuterJoinForMultitableInheritance()) { Expression childrenCriteria = getDescriptor().getInheritancePolicy().getChildrenJoinExpression(); childrenCriteria = getBaseExpression().twist(childrenCriteria, this); childrenCriteria.convertToUseOuterJoin(); if(criteria == null) { criteria = childrenCriteria; } else { criteria = criteria.and(childrenCriteria); } } } return criteria; }
/** * INTERNAL: * Return the expression to join the main table of this node to any auxiliary tables. */ public Expression additionalExpressionCriteria() { if (getDescriptor() == null) { return null; } Expression criteria = getDescriptor().getQueryManager().getAdditionalJoinExpression(); if(getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { if(isUsingOuterJoinForMultitableInheritance()) { Expression childrenCriteria = getDescriptor().getInheritancePolicy().getChildrenJoinExpression(); childrenCriteria = getBaseExpression().twist(childrenCriteria, this); childrenCriteria.convertToUseOuterJoin(); if(criteria == null) { criteria = childrenCriteria; } else { criteria = criteria.and(childrenCriteria); } } } return criteria; }