Search in sources :

Example 16 with Association

use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.

the class AbstractSqlLikeQueryBuilder method handleAssociationCriteria.

private void handleAssociationCriteria(CriteriaContext ctx, AssociationQuery associationQuery) {
    QueryState queryState = ctx.getQueryState();
    Association association = associationQuery.getAssociation();
    if (association == null) {
        return;
    }
    // Join should be applied in `findProperty` only in cases when it's needed; we don't need to join to access the identity
    String associationPath = associationQuery.getPath();
    CriteriaContext associatedContext = new CriteriaContext() {

        @Override
        public String getCurrentTableAlias() {
            return ctx.getCurrentTableAlias();
        }

        @Override
        public QueryState getQueryState() {
            return ctx.getQueryState();
        }

        @Override
        public PersistentEntity getPersistentEntity() {
            return ctx.getPersistentEntity();
        }

        @Override
        public QueryPropertyPath getRequiredProperty(String name, Class<?> criterionClazz) {
            if (StringUtils.isNotEmpty(associationPath)) {
                name = associationPath + DOT + name;
            }
            return findPropertyInternal(queryState, getPersistentEntity(), getCurrentTableAlias(), name, criterionClazz);
        }
    };
    handleJunction(associatedContext, associationQuery.getCriteria());
}
Also used : Association(io.micronaut.data.model.Association)

Example 17 with Association

use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.

the class JpaQueryBuilder method buildJoin.

@Override
protected String[] buildJoin(String alias, JoinPath joinPath, String joinType, StringBuilder target, Map<String, String> appliedJoinPaths, QueryState queryState) {
    Association[] associationPath = joinPath.getAssociationPath();
    String[] joinAliases;
    if (ArrayUtils.isEmpty(associationPath)) {
        throw new IllegalArgumentException("Invalid association path [" + joinPath.getPath() + "]");
    }
    List<Association> joinAssociationsPath = new ArrayList<>(associationPath.length);
    joinAliases = new String[associationPath.length];
    StringJoiner pathSoFar = new StringJoiner(".");
    List<String> aliases = new ArrayList<>();
    for (int i = 0; i < associationPath.length; i++) {
        Association association = associationPath[i];
        pathSoFar.add(association.getName());
        if (association instanceof Embedded) {
            joinAssociationsPath.add(association);
            continue;
        }
        String currentPath = pathSoFar.toString();
        String existingAlias = appliedJoinPaths.get(currentPath);
        if (existingAlias != null) {
            joinAliases[i] = existingAlias;
            aliases.add(existingAlias);
        } else {
            int finalI = i;
            JoinPath joinPathToUse = queryState.getQueryModel().getJoinPath(currentPath).orElseGet(() -> new JoinPath(currentPath, Arrays.copyOfRange(associationPath, 0, finalI + 1), joinPath.getJoinType(), joinPath.getAlias().orElse(null)));
            String currentAlias = getAliasName(joinPathToUse);
            joinAliases[i] = currentAlias;
            String lastJoinAlias = aliases.isEmpty() ? alias : CollectionUtils.last(aliases);
            target.append(joinType).append(lastJoinAlias).append(DOT).append(association.getName()).append(SPACE).append(joinAliases[i]);
            aliases.add(currentAlias);
        }
        joinAssociationsPath.clear();
    }
    return joinAliases;
}
Also used : Association(io.micronaut.data.model.Association) JoinPath(io.micronaut.data.model.query.JoinPath) ArrayList(java.util.ArrayList) Embedded(io.micronaut.data.model.Embedded) StringJoiner(java.util.StringJoiner)

Example 18 with Association

use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.

the class NamingStrategy method mappedJoinTableColumn.

default String mappedJoinTableColumn(PersistentEntity associated, List<Association> associations, PersistentProperty property) {
    StringBuilder sb = new StringBuilder();
    sb.append(associated.getDecapitalizedName());
    for (Association association : associations) {
        sb.append(NameUtils.capitalize(association.getName()));
    }
    if (associations.isEmpty()) {
        sb.append(getForeignKeySuffix());
    } else {
        sb.append(NameUtils.capitalize(property.getName()));
    }
    return mappedName(sb.toString());
}
Also used : Association(io.micronaut.data.model.Association)

Example 19 with Association

use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.

the class QueryParameter method asStringPath.

private String[] asStringPath(List<Association> associations, PersistentProperty property) {
    if (associations.isEmpty()) {
        return new String[] { property.getName() };
    }
    List<String> path = new ArrayList<>(associations.size() + 1);
    for (Association association : associations) {
        path.add(association.getName());
    }
    path.add(property.getName());
    return path.toArray(new String[0]);
}
Also used : Association(io.micronaut.data.model.Association) ArrayList(java.util.ArrayList)

Example 20 with Association

use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.

the class UpdateMethodMatcher method entityUpdate.

private UpdateCriteriaMethodMatch entityUpdate(java.util.regex.Matcher matcher, ParameterElement entityParameter, ParameterElement entitiesParameter) {
    return new UpdateCriteriaMethodMatch(matcher) {

        ParameterElement entityParam = entityParameter == null ? entitiesParameter : entityParameter;

        @Override
        protected <T> void applyPredicates(List<ParameterElement> parameters, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
            final SourcePersistentEntity rootEntity = (SourcePersistentEntity) root.getPersistentEntity();
            Predicate predicate;
            if (rootEntity.getVersion() != null) {
                predicate = cb.and(cb.equal(root.id(), cb.entityPropertyParameter(entityParam)), cb.equal(root.version(), cb.entityPropertyParameter(entityParam)));
            } else {
                predicate = cb.equal(root.id(), cb.entityPropertyParameter(entityParam));
            }
            query.where(predicate);
        }

        @Override
        protected <T> void addPropertiesToUpdate(MethodMatchContext matchContext, PersistentEntityRoot<T> root, PersistentEntityCriteriaUpdate<T> query, SourcePersistentEntityCriteriaBuilder cb) {
            final SourcePersistentEntity rootEntity = matchContext.getRootEntity();
            Stream.concat(rootEntity.getPersistentProperties().stream(), Stream.of(rootEntity.getVersion())).filter(p -> p != null && !((p instanceof Association) && ((Association) p).isForeignKey()) && !p.isGenerated() && p.findAnnotation(AutoPopulated.class).map(ap -> ap.getRequiredValue(AutoPopulated.UPDATEABLE, Boolean.class)).orElse(true)).forEach(p -> query.set(p.getName(), cb.entityPropertyParameter(entityParam)));
            if (((AbstractPersistentEntityCriteriaUpdate<T>) query).getUpdateValues().isEmpty()) {
                // Workaround for only ID entities
                query.set(rootEntity.getIdentity().getName(), cb.entityPropertyParameter(entityParam));
            }
        }

        @Override
        protected boolean supportedByImplicitQueries() {
            return true;
        }

        @Override
        protected Map.Entry<ClassElement, Class<? extends DataInterceptor>> resolveReturnTypeAndInterceptor(MethodMatchContext matchContext) {
            Map.Entry<ClassElement, Class<? extends DataInterceptor>> e = super.resolveReturnTypeAndInterceptor(matchContext);
            ClassElement returnType = e.getKey();
            if (returnType != null && !TypeUtils.isVoid(returnType) && !TypeUtils.isNumber(returnType) && !returnType.hasStereotype(MappedEntity.class) && !(TypeUtils.isReactiveOrFuture(matchContext.getReturnType()) && TypeUtils.isObjectClass(returnType))) {
                throw new MatchFailedException("Cannot implement update method for specified return type: " + returnType.getName());
            }
            return e;
        }

        @Override
        protected ParameterElement getEntityParameter() {
            return entityParameter;
        }

        @Override
        protected ParameterElement getEntitiesParameter() {
            return entitiesParameter;
        }
    };
}
Also used : SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) Arrays(java.util.Arrays) Parameter(io.micronaut.context.annotation.Parameter) MappedEntity(io.micronaut.data.annotation.MappedEntity) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) Internal(io.micronaut.core.annotation.Internal) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) ParameterElement(io.micronaut.inject.ast.ParameterElement) Version(io.micronaut.data.annotation.Version) Map(java.util.Map) Id(io.micronaut.data.annotation.Id) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) UpdateCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.UpdateCriteriaMethodMatch) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) Predicate(jakarta.persistence.criteria.Predicate) AutoPopulated(io.micronaut.data.annotation.AutoPopulated) Set(java.util.Set) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) Collectors(java.util.stream.Collectors) PersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) List(java.util.List) Association(io.micronaut.data.model.Association) Stream(java.util.stream.Stream) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) MethodElement(io.micronaut.inject.ast.MethodElement) DataInterceptor(io.micronaut.data.intercept.DataInterceptor) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) MappedEntity(io.micronaut.data.annotation.MappedEntity) ClassElement(io.micronaut.inject.ast.ClassElement) Predicate(jakarta.persistence.criteria.Predicate) SourcePersistentEntityCriteriaBuilder(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaBuilder) MethodMatchContext(io.micronaut.data.processor.visitors.MethodMatchContext) Association(io.micronaut.data.model.Association) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) AbstractPersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaUpdate) PersistentEntityCriteriaUpdate(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaUpdate) UpdateCriteriaMethodMatch(io.micronaut.data.processor.visitors.finders.criteria.UpdateCriteriaMethodMatch) List(java.util.List) ParameterElement(io.micronaut.inject.ast.ParameterElement) Map(java.util.Map) AutoPopulated(io.micronaut.data.annotation.AutoPopulated)

Aggregations

Association (io.micronaut.data.model.Association)46 PersistentProperty (io.micronaut.data.model.PersistentProperty)21 ArrayList (java.util.ArrayList)21 Embedded (io.micronaut.data.model.Embedded)17 StringJoiner (java.util.StringJoiner)16 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)15 NonNull (io.micronaut.core.annotation.NonNull)14 PersistentEntity (io.micronaut.data.model.PersistentEntity)14 List (java.util.List)14 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)13 Relation (io.micronaut.data.annotation.Relation)11 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)11 JoinPath (io.micronaut.data.model.query.JoinPath)11 Map (java.util.Map)11 HashMap (java.util.HashMap)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 MappedEntity (io.micronaut.data.annotation.MappedEntity)9 QueryModel (io.micronaut.data.model.query.QueryModel)9 Collections (java.util.Collections)9