Skip to content

Allow dynamic projection for Neo4jTemplate::save #2420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Andy2003 opened this issue Nov 2, 2021 · 1 comment
Closed

Allow dynamic projection for Neo4jTemplate::save #2420

Andy2003 opened this issue Nov 2, 2021 · 1 comment
Assignees
Labels
type: new-feature A completely new feature

Comments

@Andy2003
Copy link
Contributor

Andy2003 commented Nov 2, 2021

There are situations when you want to dynamically determine which fields of a domain object should actually be saved.

To achieve this it would be good to have a method similar to Neo4jTemplate::saveImpl available as a public API

private <T> T saveImpl(T instance, @Nullable Map<PropertyPath, Boolean> includedProperties, @Nullable NestedRelationshipProcessingStateMachine stateMachine) {
if (stateMachine != null && stateMachine.hasProcessedValue(instance)) {
return instance;
}
Neo4jPersistentEntity<?> entityMetaData = neo4jMappingContext.getRequiredPersistentEntity(instance.getClass());
boolean isEntityNew = entityMetaData.isNew(instance);
T entityToBeSaved = eventSupport.maybeCallBeforeBind(instance);
DynamicLabels dynamicLabels = determineDynamicLabels(entityToBeSaved, entityMetaData);
@SuppressWarnings("unchecked") // Applies to retrieving the meta data
TemplateSupport.FilteredBinderFunction<T> binderFunction = TemplateSupport.createAndApplyPropertyFilter(
includedProperties, entityMetaData,
neo4jMappingContext.getRequiredBinderFunctionFor((Class<T>) entityToBeSaved.getClass())
);
Optional<Entity> newOrUpdatedNode = neo4jClient
.query(() -> renderer.render(cypherGenerator.prepareSaveOf(entityMetaData, dynamicLabels)))
.bind(entityToBeSaved)
.with(binderFunction)
.fetchAs(Entity.class)
.one();
if (!newOrUpdatedNode.isPresent()) {
if (entityMetaData.hasVersionProperty()) {
throw new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE);
}
// defensive exception throwing
throw new IllegalStateException("Could not retrieve an internal id while saving.");
}
Long internalId = newOrUpdatedNode.get().id();
PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(entityToBeSaved);
if (entityMetaData.isUsingInternalIds()) {
propertyAccessor.setProperty(entityMetaData.getRequiredIdProperty(), internalId);
}
TemplateSupport.updateVersionPropertyIfPossible(entityMetaData, propertyAccessor, newOrUpdatedNode.get());
if (stateMachine == null) {
stateMachine = new NestedRelationshipProcessingStateMachine(neo4jMappingContext, instance, internalId);
}
stateMachine.markValueAsProcessed(instance, internalId);
processRelations(entityMetaData, propertyAccessor, isEntityNew, stateMachine, binderFunction.filter);
T bean = propertyAccessor.getBean();
stateMachine.markValueAsProcessedAs(instance, bean);
return bean;
}

@Andy2003
Copy link
Contributor Author

Is there a possibility of downporting this to 6.2 so we don't have to wait for the next spring boot release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: new-feature A completely new feature
Projects
None yet
Development

No branches or pull requests

4 participants