use of io.micronaut.data.model.runtime.RuntimeAssociation in project micronaut-data by micronaut-projects.
the class AbstractCascadeOperations method afterCascadedOne.
/**
* Process after a child element has been cascaded.
*
* @param entity The parent entity.
* @param associations The association leading to the child
* @param prevChild The previous child value
* @param newChild The new child value
* @param <T> The entity type
* @return The entity instance
*/
protected <T> T afterCascadedOne(T entity, List<Association> associations, Object prevChild, Object newChild) {
RuntimeAssociation<Object> association = (RuntimeAssociation<Object>) associations.iterator().next();
if (associations.size() == 1) {
if (association.isForeignKey()) {
PersistentAssociationPath inverse = association.getInversePathSide().orElse(null);
if (inverse != null) {
// don't cast to BeanProperty<T..> here because its the inverse, so we want to set the entity onto the newChild
newChild = inverse.setPropertyValue(newChild, entity);
}
}
if (prevChild != newChild) {
entity = setProperty((BeanProperty<T, Object>) association.getProperty(), entity, newChild);
}
return entity;
} else {
BeanProperty<T, Object> property = (BeanProperty<T, Object>) association.getProperty();
Object innerEntity = property.get(entity);
Object newInnerEntity = afterCascadedOne(innerEntity, associations.subList(1, associations.size()), prevChild, newChild);
if (newInnerEntity != innerEntity) {
innerEntity = convertAndSetWithValue(property, entity, newInnerEntity);
}
return (T) innerEntity;
}
}
use of io.micronaut.data.model.runtime.RuntimeAssociation in project micronaut-data by micronaut-projects.
the class DefaultMongoRepositoryOperations method triggerPostLoad.
private <K> K triggerPostLoad(AnnotationMetadata annotationMetadata, RuntimePersistentEntity<K> persistentEntity, K entity) {
if (persistentEntity.hasPostLoadEventListeners()) {
entity = triggerPostLoad(entity, persistentEntity, annotationMetadata);
}
for (PersistentProperty pp : persistentEntity.getPersistentProperties()) {
if (pp instanceof RuntimeAssociation) {
RuntimeAssociation runtimeAssociation = (RuntimeAssociation) pp;
Object o = runtimeAssociation.getProperty().get(entity);
if (o == null) {
continue;
}
RuntimePersistentEntity associatedEntity = runtimeAssociation.getAssociatedEntity();
switch(runtimeAssociation.getKind()) {
case MANY_TO_MANY:
case ONE_TO_MANY:
if (o instanceof Iterable) {
for (Object value : ((Iterable) o)) {
triggerPostLoad(value, associatedEntity, annotationMetadata);
}
}
continue;
case MANY_TO_ONE:
case ONE_TO_ONE:
case EMBEDDED:
triggerPostLoad(o, associatedEntity, annotationMetadata);
continue;
default:
throw new IllegalStateException("Unknown kind: " + runtimeAssociation.getKind());
}
}
}
return entity;
}
use of io.micronaut.data.model.runtime.RuntimeAssociation in project micronaut-data by micronaut-projects.
the class DefaultReactiveMongoRepositoryOperations method triggerPostLoad.
private <K> K triggerPostLoad(AnnotationMetadata annotationMetadata, RuntimePersistentEntity<K> persistentEntity, K entity) {
if (persistentEntity.hasPostLoadEventListeners()) {
entity = triggerPostLoad(entity, persistentEntity, annotationMetadata);
}
for (PersistentProperty pp : persistentEntity.getPersistentProperties()) {
if (pp instanceof RuntimeAssociation) {
RuntimeAssociation runtimeAssociation = (RuntimeAssociation) pp;
Object o = runtimeAssociation.getProperty().get(entity);
if (o == null) {
continue;
}
RuntimePersistentEntity associatedEntity = runtimeAssociation.getAssociatedEntity();
switch(runtimeAssociation.getKind()) {
case MANY_TO_MANY:
case ONE_TO_MANY:
if (o instanceof Iterable) {
for (Object value : ((Iterable) o)) {
triggerPostLoad(value, associatedEntity, annotationMetadata);
}
}
continue;
case MANY_TO_ONE:
case ONE_TO_ONE:
case EMBEDDED:
triggerPostLoad(o, associatedEntity, annotationMetadata);
continue;
default:
throw new IllegalStateException("Unknown kind: " + runtimeAssociation.getKind());
}
}
}
return entity;
}
use of io.micronaut.data.model.runtime.RuntimeAssociation in project micronaut-data by micronaut-projects.
the class SqlResultEntityTypeMapper method readChildren.
private void readChildren(RS rs, Object instance, Object parent, MappingContext<R> ctx) {
if (ctx.manyAssociations != null) {
Object id = readEntityId(rs, ctx);
MappingContext associatedCtx = ctx.manyAssociations.get(id);
if (associatedCtx == null) {
associatedCtx = ctx.copy();
R entity = (R) readEntity(rs, associatedCtx, parent, id);
Objects.requireNonNull(id);
ctx.associate(associatedCtx, id, entity);
} else {
readChildren(rs, instance, parent, associatedCtx);
}
return;
}
if (ctx.associations != null) {
for (Map.Entry<Association, MappingContext> e : ctx.associations.entrySet()) {
MappingContext associationCtx = e.getValue();
RuntimeAssociation runtimeAssociation = (RuntimeAssociation) e.getKey();
Object in = instance == null || !runtimeAssociation.getKind().isSingleEnded() ? null : runtimeAssociation.getProperty().get(instance);
readChildren(rs, in, instance, associationCtx);
}
}
}
use of io.micronaut.data.model.runtime.RuntimeAssociation in project micronaut-data by micronaut-projects.
the class SqlResultEntityTypeMapper method setChildrenAndTriggerPostLoad.
private Object setChildrenAndTriggerPostLoad(Object instance, MappingContext<?> ctx, Object parent) {
if (ctx.manyAssociations != null) {
List<Object> values = new ArrayList<>(ctx.manyAssociations.size());
for (MappingContext associationCtx : ctx.manyAssociations.values()) {
values.add(setChildrenAndTriggerPostLoad(associationCtx.entity, associationCtx, parent));
}
return values;
} else if (ctx.associations != null) {
for (Map.Entry<Association, MappingContext> e : ctx.associations.entrySet()) {
MappingContext associationCtx = e.getValue();
RuntimeAssociation runtimeAssociation = (RuntimeAssociation) e.getKey();
BeanProperty beanProperty = runtimeAssociation.getProperty();
if (runtimeAssociation.getKind().isSingleEnded() && (associationCtx.manyAssociations == null || associationCtx.manyAssociations.isEmpty())) {
Object value = beanProperty.get(instance);
Object newValue = setChildrenAndTriggerPostLoad(value, associationCtx, instance);
if (newValue != value) {
instance = setProperty(beanProperty, instance, newValue);
}
} else {
Object newValue = setChildrenAndTriggerPostLoad(null, associationCtx, instance);
newValue = resultReader.convertRequired(newValue == null ? new ArrayList<>() : newValue, beanProperty.getType());
instance = setProperty(beanProperty, instance, newValue);
}
}
}
if (instance != null && (ctx.association == null || ctx.jp != null)) {
if (parent != null && ctx.association != null && ctx.association.isBidirectional()) {
PersistentAssociationPath inverse = ctx.association.getInversePathSide().orElseThrow(IllegalStateException::new);
Association association = inverse.getAssociation();
if (association.getKind().isSingleEnded()) {
Object inverseInstance = inverse.getPropertyValue(instance);
if (inverseInstance != parent) {
instance = inverse.setPropertyValue(instance, parent);
}
}
}
triggerPostLoad(ctx.persistentEntity, instance);
}
return instance;
}
Aggregations