Search in sources :

Example 1 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class JpaUtils method expandBindings.

public static void expandBindings(Map<String, Integer> bindingMap, Map<String, String> columnBindingMap, Map<String, ExtendedAttribute<?, ?>> attributeEntries, ClauseType clause, AbstractCommonQueryBuilder<?, ?, ?, ?, ?> queryBuilder, String keyFunctionExpression, boolean enableElementCollectionIdCutoff) {
    SelectManager<?> selectManager = queryBuilder.selectManager;
    JoinManager joinManager = queryBuilder.joinManager;
    ParameterManager parameterManager = queryBuilder.parameterManager;
    JpaProvider jpaProvider = queryBuilder.mainQuery.jpaProvider;
    EntityMetamodelImpl metamodel = queryBuilder.mainQuery.metamodel;
    boolean requiresNullCast = queryBuilder.mainQuery.dbmsDialect.requiresNullCast();
    boolean needsCastParameters = queryBuilder.mainQuery.dbmsDialect.needsCastParameters();
    JpaMetamodelAccessor jpaMetamodelAccessor = jpaProvider.getJpaMetamodelAccessor();
    boolean needsElementCollectionIdCutoff = enableElementCollectionIdCutoff && jpaProvider.needsElementCollectionIdCutoff();
    final Queue<String> attributeQueue = new ArrayDeque<>(bindingMap.keySet());
    while (!attributeQueue.isEmpty()) {
        final String attributeName = attributeQueue.remove();
        Integer tupleIndex = bindingMap.get(attributeName);
        Class<?> elementType;
        String columnType;
        boolean splitExpression;
        ExtendedAttribute<?, ?> attributeEntry = attributeEntries.get(attributeName);
        if (attributeEntry == null) {
            if (!attributeName.equalsIgnoreCase(keyFunctionExpression)) {
                continue;
            }
            String realAttributeName = attributeName.substring(attributeName.indexOf('(') + 1, attributeName.length() - 1);
            attributeEntry = attributeEntries.get(realAttributeName);
            if (attributeEntry.getAttribute() instanceof ListAttribute<?, ?>) {
                elementType = Integer.class;
                columnType = queryBuilder.mainQuery.dbmsDialect.getSqlType(Integer.class);
            } else {
                MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attributeEntry.getAttribute();
                elementType = mapAttribute.getKeyJavaType();
                columnType = attributeEntry.getJoinTable() != null && attributeEntry.getJoinTable().getKeyColumnTypes() != null && attributeEntry.getJoinTable().getKeyColumnTypes().size() == 1 ? attributeEntry.getJoinTable().getKeyColumnTypes().values().iterator().next() : null;
            }
            splitExpression = false;
        } else {
            elementType = attributeEntry.getElementClass();
            columnType = attributeEntry.getColumnTypes().length == 0 ? null : attributeEntry.getColumnTypes()[0];
            final List<Attribute<?, ?>> attributePath = attributeEntry.getAttributePath();
            final Attribute<?, ?> lastAttribute = attributePath.get(attributePath.size() - 1);
            splitExpression = lastAttribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED;
            if (!splitExpression) {
                if ((clause != ClauseType.SET || jpaProvider.supportsUpdateSetAssociationId()) && jpaMetamodelAccessor.isJoinable(lastAttribute) && !isBasicElementType(lastAttribute)) {
                    splitExpression = true;
                    if (needsElementCollectionIdCutoff) {
                        OUTER: for (int i = 0; i < attributePath.size() - 1; i++) {
                            Attribute<?, ?> attribute = attributePath.get(i);
                            if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ELEMENT_COLLECTION) {
                                // This is a special case, when an embeddable is between an element collection and the association, we still need to split the expression
                                for (int j = i + 1; j < attributePath.size() - 1; j++) {
                                    attribute = attributePath.get(j);
                                    if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED) {
                                        break OUTER;
                                    }
                                }
                                splitExpression = false;
                                break;
                            }
                        }
                    }
                }
            }
        }
        SelectInfo selectInfo = selectManager.getSelectInfos().get(tupleIndex);
        final Expression selectExpression = selectInfo.getExpression();
        if (splitExpression) {
            // TODO: Maybe also allow Treat, Case-When, Array?
            if (selectExpression instanceof NullExpression) {
                final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, needsElementCollectionIdCutoff, false);
                if (embeddedPropertyNames.size() > 0) {
                    selectManager.getSelectInfos().remove(tupleIndex.intValue());
                    bindingMap.remove(attributeName);
                    // We are going to insert the expanded attributes as new select items and shift existing ones
                    int delta = embeddedPropertyNames.size() - 1;
                    if (delta > 0) {
                        for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
                            if (entry.getValue() > tupleIndex) {
                                entry.setValue(entry.getValue() + delta);
                            }
                        }
                    }
                    int offset = 0;
                    for (String embeddedPropertyName : embeddedPropertyNames) {
                        String nestedAttributePath = attributeName + "." + embeddedPropertyName;
                        ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
                        // Process the nested attribute path recursively
                        attributeQueue.add(nestedAttributePath);
                        // Replace this binding in the binding map, additional selects need an updated index
                        bindingMap.put(nestedAttributePath, tupleIndex + offset);
                        selectManager.select(offset == 0 ? selectExpression : selectExpression.copy(ExpressionCopyContext.EMPTY), null, tupleIndex + offset);
                        if (columnBindingMap != null) {
                            for (String column : nestedAttributeEntry.getColumnNames()) {
                                columnBindingMap.put(column, nestedAttributePath);
                            }
                        }
                        offset++;
                    }
                }
            } else if (selectExpression instanceof PathExpression) {
                boolean firstBinding = true;
                final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, needsElementCollectionIdCutoff, false);
                PathExpression baseExpression = embeddedPropertyNames.size() > 1 ? ((PathExpression) selectExpression).copy(ExpressionCopyContext.EMPTY) : ((PathExpression) selectExpression);
                joinManager.implicitJoin(baseExpression, true, true, true, null, ClauseType.SELECT, new HashSet<String>(), false, false, false, false);
                if (elementType != baseExpression.getPathReference().getType().getJavaType()) {
                    throw new IllegalStateException("An association should be bound to its association type and not its identifier type");
                }
                if (embeddedPropertyNames.size() > 0) {
                    bindingMap.remove(attributeName);
                    // We are going to insert the expanded attributes as new select items and shift existing ones
                    int delta = embeddedPropertyNames.size() - 1;
                    if (delta > 0) {
                        for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
                            if (entry.getValue() > tupleIndex) {
                                entry.setValue(entry.getValue() + delta);
                            }
                        }
                    }
                    int offset = 0;
                    for (String embeddedPropertyName : embeddedPropertyNames) {
                        PathExpression pathExpression = firstBinding ? ((PathExpression) selectExpression) : baseExpression.copy(ExpressionCopyContext.EMPTY);
                        for (String propertyNamePart : embeddedPropertyName.split("\\.")) {
                            pathExpression.getExpressions().add(new PropertyExpression(propertyNamePart));
                        }
                        String nestedAttributePath = attributeName + "." + embeddedPropertyName;
                        ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
                        // Process the nested attribute path recursively
                        attributeQueue.add(nestedAttributePath);
                        // Replace this binding in the binding map, additional selects need an updated index
                        bindingMap.put(nestedAttributePath, firstBinding ? tupleIndex : tupleIndex + offset);
                        if (!firstBinding) {
                            selectManager.select(pathExpression, null, tupleIndex + offset);
                        } else {
                            firstBinding = false;
                        }
                        if (columnBindingMap != null) {
                            for (String column : nestedAttributeEntry.getColumnNames()) {
                                columnBindingMap.put(column, nestedAttributePath);
                            }
                        }
                        offset++;
                    }
                }
            } else if (selectExpression instanceof ParameterExpression) {
                final Collection<String> embeddedPropertyNames = getEmbeddedPropertyPaths(attributeEntries, attributeName, jpaProvider.needsElementCollectionIdCutoff(), false);
                if (embeddedPropertyNames.size() > 0) {
                    ParameterExpression parameterExpression = (ParameterExpression) selectExpression;
                    String parameterName = parameterExpression.getName();
                    Map<String, List<String>> parameterAccessPaths = new HashMap<>(embeddedPropertyNames.size());
                    ParameterValueTransformer tranformer = parameterManager.getParameter(parameterName).getTransformer();
                    if (tranformer instanceof SplittingParameterTransformer) {
                        for (String name : ((SplittingParameterTransformer) tranformer).getParameterNames()) {
                            parameterManager.unregisterParameterName(name, clause, queryBuilder);
                        }
                    }
                    selectManager.getSelectInfos().remove(tupleIndex.intValue());
                    bindingMap.remove(attributeName);
                    // We are going to insert the expanded attributes as new select items and shift existing ones
                    int delta = embeddedPropertyNames.size() - 1;
                    if (delta > 0) {
                        for (Map.Entry<String, Integer> entry : bindingMap.entrySet()) {
                            if (entry.getValue() > tupleIndex) {
                                entry.setValue(entry.getValue() + delta);
                            }
                        }
                    }
                    int offset = 0;
                    for (String embeddedPropertyName : embeddedPropertyNames) {
                        String subParamName = "_" + parameterName + "_" + embeddedPropertyName.replace('.', '_');
                        parameterManager.registerParameterName(subParamName, false, clause, queryBuilder);
                        parameterAccessPaths.put(subParamName, Arrays.asList(embeddedPropertyName.split("\\.")));
                        String nestedAttributePath = attributeName + "." + embeddedPropertyName;
                        ExtendedAttribute<?, ?> nestedAttributeEntry = attributeEntries.get(nestedAttributePath);
                        // Process the nested attribute path recursively
                        attributeQueue.add(nestedAttributePath);
                        // Replace this binding in the binding map, additional selects need an updated index
                        bindingMap.put(nestedAttributePath, tupleIndex + offset);
                        selectManager.select(new ParameterExpression(subParamName), null, tupleIndex + offset);
                        if (columnBindingMap != null) {
                            for (String column : nestedAttributeEntry.getColumnNames()) {
                                columnBindingMap.put(column, nestedAttributePath);
                            }
                        }
                        offset++;
                    }
                    parameterManager.getParameter(parameterName).setTransformer(new SplittingParameterTransformer(parameterManager, metamodel, elementType, parameterAccessPaths));
                }
            } else {
                throw new IllegalArgumentException("Illegal expression '" + selectExpression.toString() + "' for binding relation '" + attributeName + "'!");
            }
        } else if (requiresNullCast && selectExpression instanceof NullExpression) {
            if (BasicCastTypes.TYPES.contains(elementType) && queryBuilder.statementType != DbmsStatementType.INSERT) {
                // We also need a cast for parameter expressions except in the SET clause
                List<Expression> arguments = new ArrayList<>(2);
                arguments.add(selectExpression);
                if (columnType != null) {
                    arguments.add(new StringLiteral(columnType));
                }
                selectInfo.set(new FunctionExpression("CAST_" + elementType.getSimpleName(), arguments, selectExpression));
            } else {
                final EntityMetamodelImpl.AttributeExample attributeExample = metamodel.getBasicTypeExampleAttributes().get(elementType);
                if (attributeExample != null) {
                    List<Expression> arguments = new ArrayList<>(2);
                    arguments.add(new SubqueryExpression(new Subquery() {

                        @Override
                        public String getQueryString() {
                            return attributeExample.getExampleJpql() + selectExpression;
                        }
                    }));
                    if (queryBuilder.statementType != DbmsStatementType.INSERT && needsCastParameters) {
                        arguments.add(new StringLiteral(attributeExample.getAttribute().getColumnTypes()[0]));
                    }
                    selectInfo.set(new FunctionExpression(NullfnFunction.FUNCTION_NAME, arguments, selectExpression));
                }
            }
        } else if (selectExpression instanceof ParameterExpression && clause != ClauseType.SET) {
            if (BasicCastTypes.TYPES.contains(elementType) && queryBuilder.statementType != DbmsStatementType.INSERT) {
                // We also need a cast for parameter expressions except in the SET clause
                List<Expression> arguments = new ArrayList<>(2);
                arguments.add(selectExpression);
                if (columnType != null) {
                    arguments.add(new StringLiteral(columnType));
                }
                selectInfo.set(new FunctionExpression("CAST_" + elementType.getSimpleName(), arguments, selectExpression));
            } else {
                final EntityMetamodelImpl.AttributeExample attributeExample = metamodel.getBasicTypeExampleAttributes().get(elementType);
                if (attributeExample != null) {
                    List<Expression> arguments = new ArrayList<>(2);
                    arguments.add(new SubqueryExpression(new Subquery() {

                        @Override
                        public String getQueryString() {
                            return attributeExample.getExampleJpql() + selectExpression;
                        }
                    }));
                    if (queryBuilder.statementType != DbmsStatementType.INSERT && needsCastParameters) {
                        arguments.add(new StringLiteral(attributeExample.getAttribute().getColumnTypes()[0]));
                    }
                    selectInfo.set(new FunctionExpression(ParamFunction.FUNCTION_NAME, arguments, selectExpression));
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) JpaMetamodelAccessor(com.blazebit.persistence.spi.JpaMetamodelAccessor) NullExpression(com.blazebit.persistence.parser.expression.NullExpression) MapAttribute(javax.persistence.metamodel.MapAttribute) StringLiteral(com.blazebit.persistence.parser.expression.StringLiteral) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) MapAttribute(javax.persistence.metamodel.MapAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Subquery(com.blazebit.persistence.parser.expression.Subquery) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) JpaProvider(com.blazebit.persistence.spi.JpaProvider) ArrayDeque(java.util.ArrayDeque) Expression(com.blazebit.persistence.parser.expression.Expression) QualifiedExpression(com.blazebit.persistence.parser.expression.QualifiedExpression) TreatExpression(com.blazebit.persistence.parser.expression.TreatExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) NullExpression(com.blazebit.persistence.parser.expression.NullExpression) FunctionExpression(com.blazebit.persistence.parser.expression.FunctionExpression) SubqueryExpression(com.blazebit.persistence.parser.expression.SubqueryExpression) ParameterExpression(com.blazebit.persistence.parser.expression.ParameterExpression) ListAttribute(javax.persistence.metamodel.ListAttribute)

Example 2 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class ResolvingQueryGenerator method visit.

@Override
public void visit(PathExpression expression) {
    if (resolveSelectAliases && expression.getExpressions().size() == 1) {
        AliasInfo aliasInfo;
        String potentialAlias = expression.getExpressions().get(0).toString();
        try {
            if (currentlyResolvingAliases.add(potentialAlias) && (aliasInfo = aliasManager.getAliasInfo(potentialAlias)) != null) {
                if (aliasInfo instanceof SelectInfo) {
                    SelectInfo selectAliasInfo = (SelectInfo) aliasInfo;
                    if (selectAliasInfo.getExpression() instanceof PathExpression) {
                        PathExpression aliasedExpression = (PathExpression) selectAliasInfo.getExpression();
                        boolean collectionKeyPath = aliasedExpression.isCollectionQualifiedPath();
                        boolean usedInCollectionFunction = aliasedExpression.isUsedInCollectionFunction();
                        aliasedExpression.setCollectionQualifiedPath(expression.isCollectionQualifiedPath());
                        aliasedExpression.setUsedInCollectionFunction(expression.isUsedInCollectionFunction());
                        try {
                            selectAliasInfo.getExpression().accept(this);
                        } finally {
                            aliasedExpression.setCollectionQualifiedPath(collectionKeyPath);
                            aliasedExpression.setUsedInCollectionFunction(usedInCollectionFunction);
                        }
                    } else {
                        selectAliasInfo.getExpression().accept(this);
                    }
                    return;
                }
            }
        } finally {
            currentlyResolvingAliases.remove(potentialAlias);
        }
    }
    JoinNode baseNode;
    String field;
    if ((baseNode = (JoinNode) expression.getBaseNode()) == null) {
        super.visit(expression);
    } else {
        String collectionValueFunction = jpaProvider.getCollectionValueFunction();
        if ((field = expression.getField()) == null) {
            if (expression.isUsedInCollectionFunction() || renderAbsolutePath(expression)) {
                super.visit(expression);
            } else {
                // NOTE: Hibernate uses the column from a join table if VALUE is used which is wrong, so drop the VALUE here
                boolean valueFunction = collectionValueFunction != null && needsValueFunction(expression, baseNode, field);
                if (valueFunction) {
                    sb.append(collectionValueFunction);
                    sb.append('(');
                }
                if (aliasPrefix != null) {
                    sb.append(aliasPrefix);
                }
                baseNode.appendAlias(sb, externalRepresentation);
                if (valueFunction) {
                    sb.append(')');
                }
            }
        } else {
            List<JoinNode> treatedJoinNodes = baseNode.getJoinNodesForTreatConstraint();
            Type<?> baseNodeType = baseNode.getBaseType();
            boolean addTypeCaseWhen = false;
            if (!treatedJoinNodes.isEmpty() && baseNodeType instanceof EntityType<?> && baseNode.getTreatType() != null && (jpaProvider.supportsSubtypePropertyResolving() || !jpaProvider.supportsRootTreat())) {
                ExtendedManagedType<?> extendedManagedType = entityMetamodel.getManagedType(ExtendedManagedType.class, baseNode.getTreatType().getName());
                ExtendedAttribute<?, ?> extendedAttribute = extendedManagedType.getAttributes().get(field);
                if (extendedAttribute.isColumnShared()) {
                    // To disambiguate shared column access, we also must use the type constraint
                    addTypeCaseWhen = jpaProvider.needsTypeConstraintForColumnSharing();
                } else {
                    // If the attribute is declared by an entity sub-type of the treat target type, we don't need the case when
                    ExtendedAttribute<?, ?> baseTypeAttribute = (ExtendedAttribute<?, ?>) entityMetamodel.getManagedType(ExtendedManagedType.class, ((EntityType<?>) baseNode.getBaseType()).getName()).getAttributes().get(field);
                    addTypeCaseWhen = baseTypeAttribute != null && !baseNode.getTreatType().getJavaType().isAssignableFrom(baseTypeAttribute.getAttributePath().get(0).getDeclaringType().getJavaType());
                }
            }
            if (addTypeCaseWhen) {
                sb.append("CASE WHEN ");
                boolean first = true;
                for (int i = 0; i < treatedJoinNodes.size(); i++) {
                    JoinNode treatedJoinNode = treatedJoinNodes.get(i);
                    // we skip the type constraint as that is already applied through the join
                    if (jpaProvider.supportsTreatJoin() && treatedJoinNode.isTreatJoinNode()) {
                        continue;
                    }
                    if (first) {
                        first = false;
                    } else {
                        sb.append(" AND ");
                    }
                    sb.append("TYPE(");
                    treatedJoinNode.appendAlias(sb, externalRepresentation);
                    sb.append(") IN (");
                    for (EntityType<?> entitySubtype : entityMetamodel.getEntitySubtypes(treatedJoinNode.getTreatType())) {
                        sb.append(entitySubtype.getName());
                        sb.append(", ");
                    }
                    sb.setLength(sb.length() - 2);
                    sb.append(')');
                }
                if (first) {
                    sb.setLength(sb.length() - "CASE WHEN ".length());
                    addTypeCaseWhen = false;
                } else {
                    sb.append(" THEN ");
                }
            }
            boolean valueFunction = collectionValueFunction != null && needsValueFunction(expression, baseNode, field);
            // NOTE: There is no need to check for whether the JPA provider support implicit downcasting here
            // If it didn't, the query building would have already failed before. Here we just decide whether to render the treat or not
            boolean renderTreat = jpaProvider.supportsRootTreat();
            if (valueFunction) {
                sb.append(collectionValueFunction);
                sb.append('(');
                if (aliasPrefix != null) {
                    sb.append(aliasPrefix);
                }
                baseNode.appendAlias(sb, renderTreat, externalRepresentation);
                sb.append(')');
                sb.append(".").append(field);
            } else {
                if (aliasPrefix != null) {
                    sb.append(aliasPrefix);
                }
                baseNode.appendDeReference(sb, field, renderTreat, externalRepresentation, jpaProvider.needsElementCollectionIdCutoff());
            }
            if (addTypeCaseWhen) {
                if (jpaProvider.needsCaseWhenElseBranch()) {
                    sb.append(" ELSE NULL");
                }
                sb.append(" END");
            }
        }
    }
}
Also used : ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) EntityType(javax.persistence.metamodel.EntityType) PathExpression(com.blazebit.persistence.parser.expression.PathExpression)

Example 3 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class BlazeCriteriaBuilderRenderer method serializeSubQuery.

private Object serializeSubQuery(Object criteriaBuilder, Expression<?> expression) {
    Object result = expression.accept(new Visitor<Object, Object>() {

        @Override
        public Object visit(Constant<?> constant, Object criteriaBuilder) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object visit(FactoryExpression<?> factoryExpression, Object criteriaBuilder) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object visit(Operation<?> setOperation, Object criteriaBuilder) {
            Expression<?> lhs = setOperation.getArg(0);
            SubQueryExpression<?> lhsSubquery = lhs.accept(GetSubQueryVisitor.INSTANCE, null);
            SetOperationFlag setOperationFlag = lhsSubquery != null ? getSetOperationFlag(lhsSubquery.getMetadata()) : null;
            boolean lhsNestedSet = setOperationFlag != null && LEFT_NESTED_SET_OPERATIONS.contains(setOperation.getOperator());
            if (lhsNestedSet) {
                if (criteriaBuilder instanceof StartOngoingSetOperationBuilder) {
                    StartOngoingSetOperationBuilder<?, ?, ?> ob = (StartOngoingSetOperationBuilder<?, ?, ?>) criteriaBuilder;
                    criteriaBuilder = ob.startSet();
                } else if (criteriaBuilder instanceof SubqueryInitiator) {
                    SubqueryInitiator<?> subqueryInitiator = (SubqueryInitiator<?>) criteriaBuilder;
                    criteriaBuilder = subqueryInitiator.startSet();
                } else {
                    criteriaBuilder = criteriaBuilderFactory.startSet(entityManager, Object.class);
                }
                criteriaBuilder = setOperationFlag.getFlag().accept(this, criteriaBuilder);
                if (criteriaBuilder instanceof OngoingSetOperationBuilder) {
                    criteriaBuilder = ((OngoingSetOperationBuilder<?, ?, ?>) criteriaBuilder).endSetWith();
                    renderOrderBy(lhsSubquery.getMetadata(), (OrderByBuilder<?>) criteriaBuilder);
                    renderModifiers(lhsSubquery.getMetadata().getModifiers(), (LimitBuilder<?>) criteriaBuilder);
                    criteriaBuilder = ((BaseOngoingFinalSetOperationBuilder) criteriaBuilder).endSet();
                } else {
                    throw new UnsupportedOperationException();
                }
            } else {
                criteriaBuilder = lhs.accept(this, criteriaBuilder);
            }
            Expression<?> rhs = setOperation.getArg(1);
            SubQueryExpression<?> rhsSubquery = rhs.accept(GetSubQueryVisitor.INSTANCE, null);
            setOperationFlag = rhsSubquery != null ? getSetOperationFlag(rhsSubquery.getMetadata()) : null;
            boolean isNestedSet = setOperationFlag != null;
            SetOperationBuilder<?, ?> setOperationBuilder = (SetOperationBuilder<?, ?>) criteriaBuilder;
            switch((JPQLNextOps) setOperation.getOperator()) {
                // CHECKSTYLE:OFF: FallThrough
                case SET_UNION:
                case LEFT_NESTED_SET_UNION:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startUnion() : setOperationBuilder.union();
                    break;
                case SET_UNION_ALL:
                case LEFT_NESTED_SET_UNION_ALL:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startUnionAll() : setOperationBuilder.unionAll();
                    break;
                case SET_EXCEPT:
                case LEFT_NESTED_SET_EXCEPT:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startExcept() : setOperationBuilder.except();
                    break;
                case SET_EXCEPT_ALL:
                case LEFT_NESTED_SET_EXCEPT_ALL:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startExceptAll() : setOperationBuilder.exceptAll();
                    break;
                case SET_INTERSECT:
                case LEFT_NESTED_SET_INTERSECT:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startIntersect() : setOperationBuilder.intersect();
                    break;
                case SET_INTERSECT_ALL:
                case LEFT_NESTED_SET_INTERSECT_ALL:
                    criteriaBuilder = isNestedSet ? setOperationBuilder.startIntersectAll() : setOperationBuilder.intersectAll();
                    break;
                default:
                    throw new UnsupportedOperationException("No support for set operation " + setOperation.getOperator());
            }
            if (isNestedSet) {
                criteriaBuilder = setOperationFlag.getFlag().accept(this, criteriaBuilder);
                if (criteriaBuilder instanceof OngoingSetOperationBuilder) {
                    criteriaBuilder = ((OngoingSetOperationBuilder<?, ?, ?>) criteriaBuilder).endSetWith();
                    renderOrderBy(rhsSubquery.getMetadata(), (OrderByBuilder<?>) criteriaBuilder);
                    renderModifiers(rhsSubquery.getMetadata().getModifiers(), (LimitBuilder<?>) criteriaBuilder);
                    criteriaBuilder = ((BaseOngoingFinalSetOperationBuilder) criteriaBuilder).endSet();
                } else {
                    throw new UnsupportedOperationException();
                }
            } else {
                criteriaBuilder = rhs.accept(this, criteriaBuilder);
            }
            return criteriaBuilder;
        }

        @Override
        public Object visit(ParamExpression<?> paramExpression, Object criteriaBuilder) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object visit(Path<?> path, Object criteriaBuilder) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object visit(SubQueryExpression<?> subQuery, Object criteriaBuilder) {
            QueryMetadata subQueryMetadata = subQuery.getMetadata();
            SetOperationFlag setOperationFlag = getSetOperationFlag(subQueryMetadata);
            if (setOperationFlag != null) {
                return setOperationFlag.getFlag().accept(this, criteriaBuilder);
            }
            renderCTEs(subQueryMetadata);
            criteriaBuilder = renderJoins(subQueryMetadata, (FromBaseBuilder) criteriaBuilder);
            criteriaBuilder = renderNamedWindows(subQueryMetadata, (WindowContainerBuilder) criteriaBuilder);
            renderDistinct(subQueryMetadata, (DistinctBuilder<?>) criteriaBuilder);
            renderWhere(subQueryMetadata, (WhereBuilder<?>) criteriaBuilder);
            renderGroupBy(subQueryMetadata, (GroupByBuilder<?>) criteriaBuilder);
            renderHaving(subQueryMetadata, (HavingBuilder<?>) criteriaBuilder);
            Expression<?> select = subQueryMetadata.getProjection();
            if (select instanceof FactoryExpression<?> && criteriaBuilder instanceof FullQueryBuilder<?, ?>) {
                FactoryExpression<T> factoryExpression = (FactoryExpression<T>) select;
                FullQueryBuilder<?, ?> fullQueryBuilder = (FullQueryBuilder<?, ?>) criteriaBuilder;
                criteriaBuilder = fullQueryBuilder.selectNew(new FactoryExpressionObjectBuilder(factoryExpression));
            } else {
                List<? extends Expression<?>> projection = expandProjection(subQueryMetadata.getProjection());
                if (criteriaBuilder instanceof SelectBaseCTECriteriaBuilder) {
                    SelectBaseCTECriteriaBuilder<?> selectBaseCriteriaBuilder = (SelectBaseCTECriteriaBuilder<?>) criteriaBuilder;
                    boolean bindEntity = projection.size() == 1 && subQueryMetadata.getJoins().get(0).getTarget().accept(new JoinTargetAliasPathResolver(), null).equals(projection.get(0));
                    if (bindEntity) {
                        EntityMetamodel metamodel = criteriaBuilderFactory.getService(EntityMetamodel.class);
                        Path<?> pathExpression = (Path<?>) projection.get(0);
                        ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, pathExpression.getType());
                        Map<String, ? extends ExtendedAttribute<?, ?>> ownedSingularAttributes = managedType.getOwnedSingularAttributes();
                        for (Map.Entry<String, ? extends ExtendedAttribute<?, ?>> ownedSingularAttribute : ownedSingularAttributes.entrySet()) {
                            String attributeName = ownedSingularAttribute.getKey();
                            ExtendedAttribute<?, ?> attribute = ownedSingularAttribute.getValue();
                            if (!JpaMetamodelUtils.isAssociation(attribute.getAttribute())) {
                                final SelectBuilder<?> bindBuilder = selectBaseCriteriaBuilder.bind(attributeName);
                                BeanPath<?> beanPath = new BeanPath<Object>(attribute.getElementClass(), pathExpression, attributeName);
                                setExpressionSubqueries(beanPath, null, bindBuilder, SelectBuilderExpressionSetter.INSTANCE);
                            }
                        }
                    } else {
                        for (int i = 0; i < projection.size(); i++) {
                            Expression<?> projExpression = projection.get(i);
                            BindResolverContext bindResolverContext = new BindResolverContext();
                            projExpression = projExpression.accept(BindResolver.INSTANCE, bindResolverContext);
                            Path<?> cteAttribute = bindResolverContext.getCteAttribute();
                            String alias = bindResolverContext.getAliasString();
                            if (cteAttribute == null && cteAliases != null) {
                                cteAttribute = cteAliases.get(i);
                            }
                            if (cteAttribute != null) {
                                Path<?> cteEntityPath = cteAttribute.getRoot();
                                String relativeCteAttributePath = relativePathString(cteEntityPath, cteAttribute);
                                final SelectBuilder<?> bindBuilder = selectBaseCriteriaBuilder.bind(relativeCteAttributePath);
                                setExpressionSubqueries(projExpression, alias, bindBuilder, SelectBuilderExpressionSetter.INSTANCE);
                            } else {
                                throw new UnsupportedOperationException("Select statement should be bound to any CTE attribute");
                            }
                        }
                    }
                } else {
                    for (Expression<?> selection : projection) {
                        renderSingleSelect(selection, (SelectBuilder<?>) criteriaBuilder);
                    }
                }
            }
            renderOrderBy(subQueryMetadata, (OrderByBuilder<?>) criteriaBuilder);
            renderParameters(subQueryMetadata, (ParameterHolder<?>) criteriaBuilder);
            // Limit / offset on full query is set outside of the renderer, based on whether we're rendering a full count query or not
            if (!(criteriaBuilder instanceof Queryable)) {
                renderModifiers(subQueryMetadata.getModifiers(), (LimitBuilder<?>) criteriaBuilder);
            }
            return criteriaBuilder;
        }

        @Override
        public Object visit(TemplateExpression<?> templateExpression, Object criteriaBuilder) {
            throw new UnsupportedOperationException();
        }
    }, criteriaBuilder);
    if (result instanceof BaseOngoingSetOperationBuilder) {
        result = ((BaseOngoingSetOperationBuilder<?, ?, ?>) result).endSet();
    }
    if ((result instanceof FinalSetOperationCriteriaBuilder || result instanceof FinalSetOperationCTECriteriaBuilder || result instanceof FinalSetOperationSubqueryBuilder) && expression instanceof SubQueryExpression<?>) {
        QueryMetadata metadata = ((SubQueryExpression<?>) expression).getMetadata();
        renderOrderBy(metadata, (OrderByBuilder<?>) result);
        // Limit / offset on full query is set outside of the renderer, based on whether we're rendering a full count query or not
        if (!(criteriaBuilder instanceof Queryable)) {
            renderModifiers(metadata.getModifiers(), (LimitBuilder<?>) result);
        }
    }
    return result;
}
Also used : SubQueryExpression(com.querydsl.core.types.SubQueryExpression) QueryMetadata(com.querydsl.core.QueryMetadata) FinalSetOperationCTECriteriaBuilder(com.blazebit.persistence.FinalSetOperationCTECriteriaBuilder) BaseOngoingFinalSetOperationBuilder(com.blazebit.persistence.BaseOngoingFinalSetOperationBuilder) BeanPath(com.querydsl.core.types.dsl.BeanPath) BaseOngoingFinalSetOperationBuilder(com.blazebit.persistence.BaseOngoingFinalSetOperationBuilder) SetOperationBuilder(com.blazebit.persistence.SetOperationBuilder) StartOngoingSetOperationBuilder(com.blazebit.persistence.StartOngoingSetOperationBuilder) BaseOngoingSetOperationBuilder(com.blazebit.persistence.BaseOngoingSetOperationBuilder) OngoingSetOperationBuilder(com.blazebit.persistence.OngoingSetOperationBuilder) SelectBaseCTECriteriaBuilder(com.blazebit.persistence.SelectBaseCTECriteriaBuilder) StartOngoingSetOperationBuilder(com.blazebit.persistence.StartOngoingSetOperationBuilder) FinalSetOperationCriteriaBuilder(com.blazebit.persistence.FinalSetOperationCriteriaBuilder) FullQueryBuilder(com.blazebit.persistence.FullQueryBuilder) OrderByBuilder(com.blazebit.persistence.OrderByBuilder) List(java.util.List) ArrayList(java.util.ArrayList) SubqueryInitiator(com.blazebit.persistence.SubqueryInitiator) MultipleSubqueryInitiator(com.blazebit.persistence.MultipleSubqueryInitiator) HavingBuilder(com.blazebit.persistence.HavingBuilder) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) FinalSetOperationSubqueryBuilder(com.blazebit.persistence.FinalSetOperationSubqueryBuilder) DistinctBuilder(com.blazebit.persistence.DistinctBuilder) ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) GroupByBuilder(com.blazebit.persistence.GroupByBuilder) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ParameterHolder(com.blazebit.persistence.ParameterHolder) SetOperationFlag.getSetOperationFlag(com.blazebit.persistence.querydsl.SetOperationFlag.getSetOperationFlag) FactoryExpression(com.querydsl.core.types.FactoryExpression) Queryable(com.blazebit.persistence.Queryable) WhereBuilder(com.blazebit.persistence.WhereBuilder) SelectBuilder(com.blazebit.persistence.SelectBuilder) Path(com.querydsl.core.types.Path) BeanPath(com.querydsl.core.types.dsl.BeanPath) LimitBuilder(com.blazebit.persistence.LimitBuilder) BaseOngoingSetOperationBuilder(com.blazebit.persistence.BaseOngoingSetOperationBuilder) StartOngoingSetOperationBuilder(com.blazebit.persistence.StartOngoingSetOperationBuilder) BaseOngoingSetOperationBuilder(com.blazebit.persistence.BaseOngoingSetOperationBuilder) OngoingSetOperationBuilder(com.blazebit.persistence.OngoingSetOperationBuilder) ParamExpression(com.querydsl.core.types.ParamExpression) Expression(com.querydsl.core.types.Expression) TemplateExpression(com.querydsl.core.types.TemplateExpression) FactoryExpression(com.querydsl.core.types.FactoryExpression) SubQueryExpression(com.querydsl.core.types.SubQueryExpression) JoinExpression(com.querydsl.core.JoinExpression) EntityMetamodel(com.blazebit.persistence.parser.EntityMetamodel)

Example 4 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class AbstractModificationCriteriaBuilder method prepareAndGetColumnNames.

protected List<String> prepareAndGetColumnNames() {
    StringBuilder sb = null;
    for (ExtendedAttribute entry : attributeEntries.values()) {
        for (String column : entry.getColumnNames()) {
            if (!columnBindingMap.containsKey(column)) {
                if (sb == null) {
                    sb = new StringBuilder();
                    sb.append("[");
                } else {
                    sb.append(", ");
                }
                sb.append(column);
            }
        }
    }
    if (sb != null) {
        sb.insert(0, "The following column names have not been bound: ");
        sb.append("]");
        throw new IllegalStateException(sb.toString());
    }
    return new ArrayList<>(columnBindingMap.keySet());
}
Also used : ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ArrayList(java.util.ArrayList)

Example 5 with ExtendedAttribute

use of com.blazebit.persistence.spi.ExtendedAttribute in project blaze-persistence by Blazebit.

the class InverseFlusher method forAttribute.

public static <E> InverseFlusher<E> forAttribute(EntityViewManagerImpl evm, Map<Object, EntityViewUpdaterImpl> localCache, ManagedViewType<?> viewType, AbstractMethodAttribute<?, ?> attribute, TypeDescriptor childTypeDescriptor, EntityViewUpdaterImpl owner, String ownerMapping) {
    if (attribute.getMappedBy() != null) {
        String attributeLocation = attribute.getLocation();
        Type<?> elementType = attribute instanceof PluralAttribute<?, ?, ?> ? ((PluralAttribute<?, ?, ?>) attribute).getElementType() : ((SingularAttribute<?, ?>) attribute).getType();
        Class<?> elementEntityClass = null;
        AttributeAccessor parentReferenceAttributeAccessor = null;
        Mapper<Object, Object> parentEntityOnChildViewMapper = null;
        Mapper<Object, Object> parentEntityOnChildEntityAddMapper = null;
        Mapper<Object, Object> parentEntityOnChildEntityRemoveMapper = null;
        TargetViewClassBasedInverseViewToEntityMapper childViewToEntityMapper = null;
        InverseEntityToEntityMapper childEntityToEntityMapper = null;
        ViewToEntityMapper parentReferenceViewToEntityMapper = new LoadOnlyViewToEntityMapper(new ReferenceEntityLoader(evm, viewType, EntityViewUpdaterImpl.createViewIdMapper(evm, localCache, viewType)), Accessors.forViewId(evm, (ViewType<?>) viewType, true), evm.getEntityIdAccessor());
        ViewToEntityMapper childReferenceViewToEntityMapper = null;
        TypeDescriptor parentReferenceTypeDescriptor = TypeDescriptor.forInverseAttribute(parentReferenceViewToEntityMapper);
        if (attribute.getWritableMappedByMappings() != null) {
            // This happens when the mapped by attribute is insertable=false and updatable=false
            if (childTypeDescriptor.isSubview()) {
                ViewType<?> childViewType = (ViewType<?>) elementType;
                elementEntityClass = childViewType.getEntityClass();
                Map<Class<?>, Mapper<Object, Object>> mappers = new HashMap<>();
                for (ManagedViewType<?> type : attribute.getViewTypes()) {
                    Mapper<Object, Object> mapper = Mappers.forViewConvertToViewAttributeMapping(evm, (ViewType<Object>) viewType, (ViewType<Object>) type, attribute.getWritableMappedByMappings(), (Mapper<Object, Object>) Mappers.forEntityAttributeMappingConvertToViewAttributeMapping(evm, viewType.getEntityClass(), type, attribute.getWritableMappedByMappings()));
                    if (mapper == null) {
                        mapper = NoopMapper.INSTANCE;
                    }
                    mappers.put(type.getJavaType(), mapper);
                }
                parentEntityOnChildViewMapper = (Mapper<Object, Object>) Mappers.targetViewClassBasedMapper(mappers);
                parentEntityOnChildEntityAddMapper = parentEntityOnChildEntityRemoveMapper = (Mapper<Object, Object>) Mappers.forEntityAttributeMapping(evm, viewType.getEntityClass(), childViewType.getEntityClass(), attribute.getWritableMappedByMappings());
                childReferenceViewToEntityMapper = new LoadOrPersistViewToEntityMapper(attributeLocation, evm, childViewType.getJavaType(), attribute.getReadOnlyAllowedSubtypes(), attribute.getPersistCascadeAllowedSubtypes(), attribute.getUpdateCascadeAllowedSubtypes(), new ReferenceEntityLoader(evm, childViewType, EntityViewUpdaterImpl.createViewIdMapper(evm, localCache, childViewType)), Accessors.forViewId(evm, childViewType, true), evm.getEntityIdAccessor(), true, owner, ownerMapping, localCache);
            } else if (childTypeDescriptor.isJpaEntity()) {
                Class<?> childType = elementType.getJavaType();
                elementEntityClass = childType;
                parentEntityOnChildViewMapper = (Mapper<Object, Object>) Mappers.forEntityAttributeMapping(evm, viewType.getEntityClass(), childType, attribute.getWritableMappedByMappings());
                parentEntityOnChildEntityAddMapper = parentEntityOnChildEntityRemoveMapper = (Mapper<Object, Object>) Mappers.forEntityAttributeMapping(evm, viewType.getEntityClass(), elementEntityClass, attribute.getWritableMappedByMappings());
            }
        } else {
            if (childTypeDescriptor.isSubview()) {
                ViewType<?> childViewType = (ViewType<?>) elementType;
                elementEntityClass = childViewType.getEntityClass();
                parentReferenceAttributeAccessor = Accessors.forEntityMapping(evm, childViewType.getEntityClass(), attribute.getMappedBy());
                childReferenceViewToEntityMapper = new LoadOrPersistViewToEntityMapper(attributeLocation, evm, childViewType.getJavaType(), attribute.getReadOnlyAllowedSubtypes(), attribute.getPersistCascadeAllowedSubtypes(), attribute.getUpdateCascadeAllowedSubtypes(), new ReferenceEntityLoader(evm, childViewType, EntityViewUpdaterImpl.createViewIdMapper(evm, localCache, childViewType)), Accessors.forViewId(evm, childViewType, true), evm.getEntityIdAccessor(), true, owner, ownerMapping, localCache);
                parentEntityOnChildEntityAddMapper = parentEntityOnChildEntityRemoveMapper = Mappers.forAccessor(parentReferenceAttributeAccessor);
                Map<Class<?>, Mapper<Object, Object>> mappers = new HashMap<>();
                for (ManagedViewType<?> type : attribute.getViewTypes()) {
                    Mapper<Object, Object> mapper = (Mapper<Object, Object>) Mappers.forViewConvertToViewAttributeMapping(evm, (ViewType<Object>) viewType, (ViewType<Object>) type, attribute.getMappedBy(), null);
                    if (mapper == null) {
                        mapper = NoopMapper.INSTANCE;
                    }
                    mappers.put(type.getJavaType(), mapper);
                }
                parentEntityOnChildViewMapper = (Mapper<Object, Object>) Mappers.targetViewClassBasedMapper(mappers);
            } else if (childTypeDescriptor.isJpaEntity()) {
                Class<?> childType = elementType.getJavaType();
                elementEntityClass = childType;
                parentReferenceAttributeAccessor = Accessors.forEntityMapping(evm, childType, attribute.getMappedBy());
                parentEntityOnChildEntityAddMapper = parentEntityOnChildEntityRemoveMapper = Mappers.forAccessor(parentReferenceAttributeAccessor);
                parentEntityOnChildViewMapper = Mappers.forAccessor(parentReferenceAttributeAccessor);
            }
        }
        DirtyAttributeFlusher<?, Object, ? extends Object> parentReferenceAttributeFlusher;
        ManagedType<?> managedType = evm.getMetamodel().getEntityMetamodel().getManagedType(elementEntityClass);
        Attribute<?, ?> inverseAttribute = JpaMetamodelUtils.getAttribute(managedType, attribute.getMappedBy());
        // Many-To-Many relation can't be handled by the inverse flushers
        if (inverseAttribute != null && inverseAttribute.isCollection()) {
            // A collection can only have a single attribute, so it's safe to assume a SimpleMapper
            parentEntityOnChildEntityAddMapper = new CollectionAddMapper<>(parentEntityOnChildEntityAddMapper == null ? parentReferenceAttributeAccessor : ((SimpleMapper<Object, Object>) parentEntityOnChildEntityAddMapper).getAttributeAccessor());
            parentEntityOnChildEntityRemoveMapper = new CollectionRemoveMapper<>(parentEntityOnChildEntityRemoveMapper == null ? parentReferenceAttributeAccessor : ((SimpleMapper<Object, Object>) parentEntityOnChildEntityRemoveMapper).getAttributeAccessor());
            parentReferenceAttributeFlusher = new ParentCollectionReferenceAttributeFlusher<>(attributeLocation, attribute.getMappedBy(), viewType.getFlushStrategy(), parentReferenceAttributeAccessor, null, null, null, TypeDescriptor.forInverseCollectionAttribute(viewType.getEntityClass(), new EntityBasicUserType(evm.getJpaProvider())));
        } else {
            parentEntityOnChildEntityRemoveMapper = new NullMapper<>(parentEntityOnChildEntityRemoveMapper);
            parentReferenceAttributeFlusher = new ParentReferenceAttributeFlusher<>(evm, viewType.getEntityClass(), attributeLocation, attribute.getMappedBy(), attribute.getWritableMappedByMappings(), parentReferenceTypeDescriptor, parentReferenceAttributeAccessor, parentEntityOnChildViewMapper);
        }
        UnmappedAttributeCascadeDeleter deleter = null;
        String parentIdAttributeName = null;
        String childIdAttributeName = null;
        Class<?> childIdViewClass = null;
        // Only construct when orphanRemoval or delete cascading is enabled, orphanRemoval implies delete cascading
        if (attribute.isDeleteCascaded()) {
            EntityMetamodel entityMetamodel = evm.getMetamodel().getEntityMetamodel();
            ExtendedManagedType<?> ownerManagedType = entityMetamodel.getManagedType(ExtendedManagedType.class, viewType.getEntityClass());
            ExtendedManagedType<?> elementManagedType = entityMetamodel.getManagedType(ExtendedManagedType.class, elementEntityClass);
            parentIdAttributeName = ownerManagedType.getIdAttribute().getName();
            childIdAttributeName = elementManagedType.getIdAttribute().getName();
            String mapping = attribute.getMappedBy();
            if (mapping != null) {
                if (mapping.isEmpty()) {
                    deleter = new UnmappedWritableBasicAttributeSetNullCascadeDeleter(evm, ownerManagedType.getType(), elementManagedType, attribute.getWritableMappedByMappings());
                } else {
                    ExtendedAttribute extendedAttribute = elementManagedType.getAttribute(mapping);
                    if (childTypeDescriptor.isSubview()) {
                        if (elementType instanceof ViewType<?>) {
                            MethodAttribute<?, ?> idAttribute = ((ViewType<?>) elementType).getIdAttribute();
                            if (idAttribute.isSubview()) {
                                // in this case, we need to fetch the id as view as the deleter expects it this way
                                childIdViewClass = idAttribute.getJavaType();
                            }
                        }
                        deleter = new ViewTypeCascadeDeleter(childTypeDescriptor.getViewToEntityMapper());
                    } else if (childTypeDescriptor.isJpaEntity()) {
                        deleter = new UnmappedBasicAttributeCascadeDeleter(evm, mapping, extendedAttribute, mapping + "." + parentIdAttributeName, false);
                    }
                }
            }
        }
        if (childTypeDescriptor.isSubview()) {
            InverseViewToEntityMapper<?> first = null;
            Map<Class<?>, InverseViewToEntityMapper<?>> mappers = new HashMap<>();
            for (ManagedViewType<?> type : attribute.getViewTypes()) {
                InverseViewToEntityMapper inverseViewToEntityMapper = new InverseViewToEntityMapper(evm, localCache, (ViewType<?>) type, parentEntityOnChildViewMapper, parentEntityOnChildEntityAddMapper, parentEntityOnChildEntityRemoveMapper, childTypeDescriptor.getViewToEntityMapper(), parentReferenceAttributeFlusher, EntityViewUpdaterImpl.createIdFlusher(evm, localCache, (ViewType<?>) type, EntityViewUpdaterImpl.createViewIdMapper(evm, localCache, type)));
                mappers.put(type.getJavaType(), inverseViewToEntityMapper);
                if (type == elementType) {
                    first = inverseViewToEntityMapper;
                }
            }
            childViewToEntityMapper = new TargetViewClassBasedInverseViewToEntityMapper(first, mappers);
        } else if (childTypeDescriptor.isJpaEntity()) {
            Class<?> childType = elementType.getJavaType();
            childEntityToEntityMapper = new InverseEntityToEntityMapper(evm, evm.getMetamodel().getEntityMetamodel().entity(childType), parentEntityOnChildEntityAddMapper, parentEntityOnChildEntityRemoveMapper, parentReferenceAttributeFlusher);
        }
        return new InverseFlusher(viewType.getEntityClass(), attribute.getMapping(), parentIdAttributeName, childIdAttributeName, childIdViewClass, deleter, parentReferenceViewToEntityMapper, parentReferenceAttributeFlusher, parentEntityOnChildViewMapper, childViewToEntityMapper, childReferenceViewToEntityMapper, parentEntityOnChildEntityAddMapper, childEntityToEntityMapper);
    }
    return null;
}
Also used : LoadOrPersistViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper) LoadOnlyViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOnlyViewToEntityMapper) HashMap(java.util.HashMap) CollectionRemoveMapper(com.blazebit.persistence.view.impl.mapper.CollectionRemoveMapper) LoadOrPersistViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper) LoadOnlyViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOnlyViewToEntityMapper) NoopMapper(com.blazebit.persistence.view.impl.mapper.NoopMapper) InverseEntityToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseEntityToEntityMapper) NullMapper(com.blazebit.persistence.view.impl.mapper.NullMapper) CollectionAddMapper(com.blazebit.persistence.view.impl.mapper.CollectionAddMapper) TargetViewClassBasedInverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.TargetViewClassBasedInverseViewToEntityMapper) InverseElementToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseElementToEntityMapper) Mapper(com.blazebit.persistence.view.impl.mapper.Mapper) SimpleMapper(com.blazebit.persistence.view.impl.mapper.SimpleMapper) ViewToEntityMapper(com.blazebit.persistence.view.impl.entity.ViewToEntityMapper) InverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseViewToEntityMapper) LoadOrPersistViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOrPersistViewToEntityMapper) LoadOnlyViewToEntityMapper(com.blazebit.persistence.view.impl.entity.LoadOnlyViewToEntityMapper) TargetViewClassBasedInverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.TargetViewClassBasedInverseViewToEntityMapper) ViewToEntityMapper(com.blazebit.persistence.view.impl.entity.ViewToEntityMapper) InverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseViewToEntityMapper) TargetViewClassBasedInverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.TargetViewClassBasedInverseViewToEntityMapper) TargetViewClassBasedInverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.TargetViewClassBasedInverseViewToEntityMapper) InverseViewToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseViewToEntityMapper) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) ReferenceEntityLoader(com.blazebit.persistence.view.impl.entity.ReferenceEntityLoader) InverseEntityToEntityMapper(com.blazebit.persistence.view.impl.entity.InverseEntityToEntityMapper) EntityBasicUserType(com.blazebit.persistence.view.impl.type.EntityBasicUserType) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) EntityMetamodel(com.blazebit.persistence.parser.EntityMetamodel) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Aggregations

ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)21 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)13 Map (java.util.Map)7 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Attribute (javax.persistence.metamodel.Attribute)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 PathReference (com.blazebit.persistence.parser.expression.PathReference)3 EntityMetamodel (com.blazebit.persistence.parser.EntityMetamodel)2 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)2 Expression (com.blazebit.persistence.parser.expression.Expression)2 FunctionExpression (com.blazebit.persistence.parser.expression.FunctionExpression)2 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)2 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)2 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)2 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2