use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class PersistentPropertyPath method getPathAsString.
@NonNull
default String getPathAsString() {
StringJoiner joiner = new StringJoiner(".");
for (Association association : getAssociations()) {
joiner.add(association.getName());
}
joiner.add(getProperty().getName());
return joiner.toString();
}
use of io.micronaut.data.model.Association 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;
}
use of io.micronaut.data.model.Association 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);
}
use of io.micronaut.data.model.Association 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));
}
use of io.micronaut.data.model.Association 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);
});
}
Aggregations