Search in sources :

Example 21 with Association

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

the class AbstractCriteriaMethodMatch method applyJoinSpecs.

protected final void applyJoinSpecs(PersistentEntityRoot<?> root, @NonNull List<AnnotationValue<Join>> joinSpecs) {
    for (AnnotationValue<Join> joinSpec : joinSpecs) {
        String path = joinSpec.stringValue().orElse(null);
        Join.Type type = joinSpec.enumValue("type", Join.Type.class).orElse(Join.Type.FETCH);
        String alias = joinSpec.stringValue("alias").orElse(null);
        if (path != null) {
            PersistentPropertyPath propertyPath = root.getPersistentEntity().getPropertyPath(path);
            if (propertyPath == null || !(propertyPath.getProperty() instanceof Association)) {
                throw new MatchFailedException("Invalid join spec [" + path + "]. Property is not an association!");
            } else {
                PersistentEntityFrom<?, ?> p = root;
                for (Association association : propertyPath.getAssociations()) {
                    p = p.join(association.getName(), type);
                }
                if (alias != null) {
                    p.join(propertyPath.getProperty().getName(), type, alias);
                } else {
                    p.join(propertyPath.getProperty().getName(), type);
                }
            }
        }
    }
}
Also used : Association(io.micronaut.data.model.Association) MatchFailedException(io.micronaut.data.processor.visitors.MatchFailedException) Join(io.micronaut.data.annotation.Join) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath)

Example 22 with Association

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

the class AbstractCriteriaMethodMatch method findProperty.

@Nullable
protected final <T> io.micronaut.data.model.jpa.criteria.PersistentPropertyPath<Object> findProperty(PersistentEntityRoot<T> root, String propertyName) {
    propertyName = NameUtils.decapitalize(propertyName);
    PersistentEntity entity = root.getPersistentEntity();
    PersistentProperty prop = entity.getPropertyByName(propertyName);
    PersistentPropertyPath pp;
    if (prop == null) {
        Optional<String> propertyPath = entity.getPath(propertyName);
        if (propertyPath.isPresent()) {
            String path = propertyPath.get();
            pp = entity.getPropertyPath(path);
            if (pp == null) {
                return null;
            }
        } else {
            return null;
        }
    } else {
        pp = PersistentPropertyPath.of(Collections.emptyList(), prop, propertyName);
    }
    PersistentEntityFrom<?, ?> path = root;
    for (Association association : pp.getAssociations()) {
        path = path.join(association.getName());
    }
    Path<Object> exp;
    if (pp.getProperty() instanceof Association && ((Association) pp.getProperty()).getKind() != Relation.Kind.EMBEDDED) {
        exp = path.join(pp.getProperty().getName());
    } else {
        exp = path.get(pp.getProperty().getName());
    }
    return CriteriaUtils.requireProperty(exp);
}
Also used : Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) PersistentProperty(io.micronaut.data.model.PersistentProperty) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) Nullable(io.micronaut.core.annotation.Nullable)

Example 23 with Association

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

the class MongoUtils method idValue.

public static BsonValue idValue(ConversionService<?> conversionService, RuntimePersistentEntity<?> persistentEntity, Object idValue, CodecRegistry codecRegistry) {
    RuntimePersistentProperty<?> identity = persistentEntity.getIdentity();
    if (identity != null) {
        if (identity instanceof Association) {
            return toBsonValue(conversionService, idValue, codecRegistry);
        }
        AnnotationValue<BsonRepresentation> bsonRepresentation = identity.getAnnotationMetadata().getAnnotation(BsonRepresentation.class);
        if (bsonRepresentation != null) {
            BsonType bsonType = bsonRepresentation.getRequiredValue(BsonType.class);
            return toBsonValue(conversionService, bsonType, idValue);
        } else {
            BeanProperty property = identity.getProperty();
            Class<?> type = property.getType();
            if (type == String.class && idValue != null) {
                return new BsonObjectId(new ObjectId(idValue.toString()));
            }
            return toBsonValue(conversionService, idValue, codecRegistry);
        }
    }
    throw new IllegalStateException("Cannot determine id!");
}
Also used : BsonType(org.bson.BsonType) Association(io.micronaut.data.model.Association) BsonRepresentation(org.bson.codecs.pojo.annotations.BsonRepresentation) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BeanProperty(io.micronaut.core.beans.BeanProperty)

Example 24 with Association

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

the class SourcePersistentEntityPath method get.

@Override
default <Y> PersistentPropertyPath<Y> get(String attributeName) {
    SourcePersistentProperty property = getPersistentEntity().getPropertyByName(attributeName);
    if (property == null) {
        throw new IllegalStateException("Cannot query entity [" + getPersistentEntity().getSimpleName() + "] on non-existent property: " + attributeName);
    }
    if (this instanceof PersistentAssociationPath) {
        PersistentAssociationPath<?, ?> associationPath = (PersistentAssociationPath) this;
        List<Association> associations = associationPath.getAssociations();
        List<Association> newAssociations = new ArrayList<>(associations.size() + 1);
        newAssociations.addAll(associations);
        newAssociations.add(associationPath.getAssociation());
        return new SourcePersistentPropertyPathImpl<>(this, newAssociations, property);
    }
    return new SourcePersistentPropertyPathImpl<>(this, Collections.emptyList(), property);
}
Also used : Association(io.micronaut.data.model.Association) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ArrayList(java.util.ArrayList) PersistentAssociationPath(io.micronaut.data.model.jpa.criteria.PersistentAssociationPath)

Example 25 with Association

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

the class SourceParameterExpressionImpl method getDataType.

private DataType getDataType(PersistentPropertyPath propertyPath, ParameterElement parameterElement) {
    if (propertyPath != null) {
        PersistentProperty property = propertyPath.getProperty();
        if (!(property instanceof Association)) {
            return property.getDataType();
        }
    }
    if (parameterElement != null) {
        DataType dataType = TypeUtils.resolveDataType(parameterElement).orElse(null);
        if (dataType != null) {
            return dataType;
        }
        ClassElement type = parameterElement.getType();
        if (TypeUtils.isContainerType(type)) {
            type = type.getFirstTypeArgument().orElse(type);
        }
        return TypeUtils.resolveDataType(type, dataTypes);
    }
    return DataType.OBJECT;
}
Also used : Association(io.micronaut.data.model.Association) DataType(io.micronaut.data.model.DataType) ClassElement(io.micronaut.inject.ast.ClassElement) PersistentProperty(io.micronaut.data.model.PersistentProperty) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty)

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