Search in sources :

Example 1 with PersistentProperty

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

the class AbstractPersistentEntityJoinSupport method addJoin.

private <X, Y> PersistentAssociationPath<X, Y> addJoin(String attributeName, io.micronaut.data.annotation.Join.Type type, String alias) {
    PersistentProperty persistentProperty = getPersistentEntity().getPropertyByName(attributeName);
    if (!(persistentProperty instanceof Association)) {
        throw new IllegalStateException("Expected an association for attribute name: " + attributeName);
    }
    PersistentAssociationPath path = joins.computeIfAbsent(attributeName, a -> createJoinAssociation((Association) persistentProperty, type, alias));
    if (type != null && type != io.micronaut.data.annotation.Join.Type.DEFAULT) {
        path.setAssociationJoinType(type);
    }
    if (alias != null) {
        path.setAlias(alias);
    }
    return (PersistentAssociationPath<X, Y>) path;
}
Also used : Association(io.micronaut.data.model.Association) PersistentProperty(io.micronaut.data.model.PersistentProperty) PersistentAssociationPath(io.micronaut.data.model.jpa.criteria.PersistentAssociationPath)

Example 2 with PersistentProperty

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

the class Joiner method joinIfNeeded.

private void joinIfNeeded(PersistentPropertyPath<?> persistentPropertyPath, boolean isPredicate) {
    PersistentProperty property = persistentPropertyPath.getProperty();
    if (isPredicate && property instanceof Association) {
        // We don't need a join to access the ID
        return;
    }
    joinAssociation(persistentPropertyPath);
}
Also used : Association(io.micronaut.data.model.Association) PersistentProperty(io.micronaut.data.model.PersistentProperty)

Example 3 with PersistentProperty

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

the class QueryModelSelectionVisitor method visit.

@Override
public void visit(IdExpression<?, ?> idExpression) {
    PersistentEntityRoot<?> root = idExpression.getRoot();
    PersistentEntity persistentEntity = root.getPersistentEntity();
    if (persistentEntity.hasCompositeIdentity()) {
        for (PersistentProperty persistentProperty : persistentEntity.getCompositeIdentity()) {
            if (distinct) {
                addProjection(Projections.distinct(persistentProperty.getName()));
            } else {
                addProjection(Projections.property(persistentProperty.getName()));
            }
        }
    } else {
        PersistentProperty identity = persistentEntity.getIdentity();
        if (distinct) {
            addProjection(Projections.distinct(identity.getName()));
        } else {
            addProjection(Projections.property(identity.getName()));
        }
    }
}
Also used : PersistentEntity(io.micronaut.data.model.PersistentEntity) PersistentProperty(io.micronaut.data.model.PersistentProperty)

Example 4 with PersistentProperty

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

the class AbstractSqlRepositoryOperations method idPropertiesWithValues.

private Stream<Map.Entry<PersistentProperty, Object>> idPropertiesWithValues(PersistentProperty property, Object value) {
    Object propertyValue = ((RuntimePersistentProperty) property).getProperty().get(value);
    if (property instanceof Embedded) {
        Embedded embedded = (Embedded) property;
        PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
        return embeddedEntity.getPersistentProperties().stream().flatMap(prop -> idPropertiesWithValues(prop, propertyValue));
    } else if (property instanceof Association) {
        Association association = (Association) property;
        if (association.isForeignKey()) {
            return Stream.empty();
        }
        PersistentEntity associatedEntity = association.getAssociatedEntity();
        PersistentProperty identity = associatedEntity.getIdentity();
        if (identity == null) {
            throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
        }
        return idPropertiesWithValues(identity, propertyValue);
    }
    return Stream.of(new AbstractMap.SimpleEntry<>(property, propertyValue));
}
Also used : AbstractMap(java.util.AbstractMap) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) Embedded(io.micronaut.data.model.Embedded) PersistentProperty(io.micronaut.data.model.PersistentProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty)

Example 5 with PersistentProperty

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

the class AbstractSqlRepositoryOperations method resolveEntityUpdate.

/**
 * Resolves a stored update for the given entity.
 *
 * @param annotationMetadata The repository annotation metadata
 * @param repositoryType     The repository type
 * @param rootEntity         The root entity
 * @param persistentEntity   The persistent entity
 * @return The insert
 */
@NonNull
protected DBOperation resolveEntityUpdate(AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull Class<?> rootEntity, @NonNull RuntimePersistentEntity<?> persistentEntity) {
    final QueryKey key = new QueryKey(repositoryType, rootEntity);
    // noinspection unchecked
    return entityUpdates.computeIfAbsent(key, (queryKey) -> {
        final SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
        final String idName;
        final PersistentProperty identity = persistentEntity.getIdentity();
        if (identity != null) {
            idName = identity.getName();
        } else {
            idName = TypeRole.ID;
        }
        final QueryModel queryModel = QueryModel.from(persistentEntity).idEq(new QueryParameter(idName));
        List<String> updateProperties = persistentEntity.getPersistentProperties().stream().filter(p -> !((p instanceof Association) && ((Association) p).isForeignKey()) && p.getAnnotationMetadata().booleanValue(AutoPopulated.class, "updateable").orElse(true)).map(PersistentProperty::getName).collect(Collectors.toList());
        final QueryResult queryResult = queryBuilder.buildUpdate(annotationMetadata, queryModel, updateProperties);
        return new QueryResultSqlOperation(queryBuilder, queryResult);
    });
}
Also used : QueryParameter(io.micronaut.data.model.query.QueryParameter) QueryResult(io.micronaut.data.model.query.builder.QueryResult) RuntimeAssociation(io.micronaut.data.model.runtime.RuntimeAssociation) Association(io.micronaut.data.model.Association) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) PersistentProperty(io.micronaut.data.model.PersistentProperty) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) QueryModel(io.micronaut.data.model.query.QueryModel) AutoPopulated(io.micronaut.data.annotation.AutoPopulated) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

PersistentProperty (io.micronaut.data.model.PersistentProperty)32 Association (io.micronaut.data.model.Association)21 PersistentEntity (io.micronaut.data.model.PersistentEntity)16 NonNull (io.micronaut.core.annotation.NonNull)14 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Embedded (io.micronaut.data.model.Embedded)12 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)12 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)11 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 MappedEntity (io.micronaut.data.annotation.MappedEntity)9 Relation (io.micronaut.data.annotation.Relation)9 Function (java.util.function.Function)9 CollectionUtils (io.micronaut.core.util.CollectionUtils)8 MappingException (io.micronaut.data.exceptions.MappingException)8 DataType (io.micronaut.data.model.DataType)8