Skip to content

Commit 5b1c633

Browse files
committed
GH-2159 - Remove DatabaseSelectionProvider from imperative template.
1 parent b95e8bd commit 5b1c633

File tree

5 files changed

+44
-66
lines changed

5 files changed

+44
-66
lines changed

src/main/java/org/springframework/data/neo4j/config/AbstractNeo4jConfig.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ public Neo4jClient neo4jClient(Driver driver, DatabaseSelectionProvider database
5959
}
6060

6161
@Bean(Neo4jRepositoryConfigurationExtension.DEFAULT_NEO4J_TEMPLATE_BEAN_NAME)
62-
public Neo4jOperations neo4jTemplate(final Neo4jClient neo4jClient, final Neo4jMappingContext mappingContext,
63-
DatabaseSelectionProvider databaseNameProvider) {
62+
public Neo4jOperations neo4jTemplate(final Neo4jClient neo4jClient, final Neo4jMappingContext mappingContext) {
6463

65-
return new Neo4jTemplate(neo4jClient, mappingContext, databaseNameProvider);
64+
return new Neo4jTemplate(neo4jClient, mappingContext);
6665
}
6766

6867
/**

src/main/java/org/springframework/data/neo4j/config/Neo4jCdiConfigurationSupport.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
*/
1616
package org.springframework.data.neo4j.config;
1717

18-
import javax.enterprise.context.ApplicationScoped;
19-
import javax.enterprise.inject.Any;
20-
import javax.enterprise.inject.Instance;
21-
import javax.enterprise.inject.Produces;
22-
import javax.inject.Singleton;
23-
2418
import org.apiguardian.api.API;
2519
import org.neo4j.driver.Driver;
2620
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
@@ -32,6 +26,12 @@
3226
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
3327
import org.springframework.transaction.PlatformTransactionManager;
3428

29+
import javax.enterprise.context.ApplicationScoped;
30+
import javax.enterprise.inject.Any;
31+
import javax.enterprise.inject.Instance;
32+
import javax.enterprise.inject.Produces;
33+
import javax.inject.Singleton;
34+
3535
/**
3636
* Support class that can be used as is for all necessary CDI beans or as a blueprint for custom producers.
3737
*
@@ -66,10 +66,9 @@ public DatabaseSelectionProvider databaseSelectionProvider() {
6666
@Produces @Builtin @Singleton
6767
public Neo4jOperations neo4jOperations(
6868
@Any Instance<Neo4jClient> neo4jClient,
69-
@Any Instance<Neo4jMappingContext> mappingContext,
70-
@Any Instance<DatabaseSelectionProvider> databaseNameProvider
69+
@Any Instance<Neo4jMappingContext> mappingContext
7170
) {
72-
return new Neo4jTemplate(resolve(neo4jClient), resolve(mappingContext), resolve(databaseNameProvider));
71+
return new Neo4jTemplate(resolve(neo4jClient), resolve(mappingContext));
7372
}
7473

7574
@Produces @Singleton

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,25 @@ public final class Neo4jTemplate implements Neo4jOperations, BeanFactoryAware {
9898

9999
private EventSupport eventSupport;
100100

101-
private final DatabaseSelectionProvider databaseSelectionProvider;
102-
103101
public Neo4jTemplate(Neo4jClient neo4jClient) {
104-
this(neo4jClient, new Neo4jMappingContext(), DatabaseSelectionProvider.getDefaultSelectionProvider());
102+
this(neo4jClient, new Neo4jMappingContext());
105103
}
106104

107-
public Neo4jTemplate(Neo4jClient neo4jClient, Neo4jMappingContext neo4jMappingContext,
108-
DatabaseSelectionProvider databaseSelectionProvider) {
105+
public Neo4jTemplate(Neo4jClient neo4jClient, Neo4jMappingContext neo4jMappingContext) {
109106

110-
this(neo4jClient, neo4jMappingContext, databaseSelectionProvider, EntityCallbacks.create());
107+
this(neo4jClient, neo4jMappingContext, EntityCallbacks.create());
111108
}
112109

113110
public Neo4jTemplate(Neo4jClient neo4jClient, Neo4jMappingContext neo4jMappingContext,
114-
DatabaseSelectionProvider databaseSelectionProvider, EntityCallbacks entityCallbacks) {
111+
EntityCallbacks entityCallbacks) {
115112

116113
Assert.notNull(neo4jClient, "The Neo4jClient is required");
117114
Assert.notNull(neo4jMappingContext, "The Neo4jMappingContext is required");
118-
Assert.notNull(databaseSelectionProvider, "The database name provider is required");
119115

120116
this.neo4jClient = neo4jClient;
121117
this.neo4jMappingContext = neo4jMappingContext;
122118
this.cypherGenerator = CypherGenerator.INSTANCE;
123119
this.eventSupport = EventSupport.useExistingCallbacks(neo4jMappingContext, entityCallbacks);
124-
125-
this.databaseSelectionProvider = databaseSelectionProvider;
126120
}
127121

128122
@Override
@@ -225,21 +219,20 @@ private Object convertIdValues(@Nullable Neo4jPersistentProperty idProperty, Obj
225219
@Override
226220
public <T> T save(T instance) {
227221

228-
return saveImpl(instance, getDatabaseName());
222+
return saveImpl(instance);
229223
}
230224

231-
private <T> T saveImpl(T instance, @Nullable String inDatabase) {
225+
private <T> T saveImpl(T instance) {
232226

233227
Neo4jPersistentEntity<?> entityMetaData = neo4jMappingContext.getPersistentEntity(instance.getClass());
234228
boolean isEntityNew = entityMetaData.isNew(instance);
235229

236230
T entityToBeSaved = eventSupport.maybeCallBeforeBind(instance);
237231

238-
DynamicLabels dynamicLabels = determineDynamicLabels(entityToBeSaved, entityMetaData, inDatabase);
232+
DynamicLabels dynamicLabels = determineDynamicLabels(entityToBeSaved, entityMetaData);
239233

240234
Optional<Long> optionalInternalId = neo4jClient
241235
.query(() -> renderer.render(cypherGenerator.prepareSaveOf(entityMetaData, dynamicLabels)))
242-
.in(inDatabase)
243236
.bind(entityToBeSaved)
244237
.with(neo4jMappingContext.getRequiredBinderFunctionFor((Class<T>) entityToBeSaved.getClass()))
245238
.fetchAs(Long.class).one();
@@ -253,17 +246,16 @@ private <T> T saveImpl(T instance, @Nullable String inDatabase) {
253246
propertyAccessor.setProperty(entityMetaData.getRequiredIdProperty(), optionalInternalId.get());
254247
entityToBeSaved = propertyAccessor.getBean();
255248
}
256-
return processRelations(entityMetaData, entityToBeSaved, isEntityNew, inDatabase);
249+
return processRelations(entityMetaData, entityToBeSaved, isEntityNew);
257250
}
258251

259-
private <T> DynamicLabels determineDynamicLabels(T entityToBeSaved, Neo4jPersistentEntity<?> entityMetaData,
260-
@Nullable String inDatabase) {
252+
private <T> DynamicLabels determineDynamicLabels(T entityToBeSaved, Neo4jPersistentEntity<?> entityMetaData) {
261253
return entityMetaData.getDynamicLabelsProperty().map(p -> {
262254

263255
PersistentPropertyAccessor propertyAccessor = entityMetaData.getPropertyAccessor(entityToBeSaved);
264256
Neo4jClient.RunnableSpecTightToDatabase runnableQuery = neo4jClient
265257
.query(() -> renderer.render(cypherGenerator.createStatementReturningDynamicLabels(entityMetaData)))
266-
.in(inDatabase).bind(propertyAccessor.getProperty(entityMetaData.getRequiredIdProperty()))
258+
.bind(propertyAccessor.getProperty(entityMetaData.getRequiredIdProperty()))
267259
.to(Constants.NAME_OF_ID).bind(entityMetaData.getStaticLabels())
268260
.to(Constants.NAME_OF_STATIC_LABELS_PARAM);
269261

@@ -282,8 +274,6 @@ private <T> DynamicLabels determineDynamicLabels(T entityToBeSaved, Neo4jPersist
282274
@Override
283275
public <T> List<T> saveAll(Iterable<T> instances) {
284276

285-
String databaseName = getDatabaseName();
286-
287277
Collection<T> entities;
288278
if (instances instanceof Collection) {
289279
entities = (Collection<T>) instances;
@@ -302,7 +292,7 @@ public <T> List<T> saveAll(Iterable<T> instances) {
302292
|| entityMetaData.getDynamicLabelsProperty().isPresent()) {
303293
log.debug("Saving entities using single statements.");
304294

305-
return entities.stream().map(e -> saveImpl(e, databaseName)).collect(Collectors.toList());
295+
return entities.stream().map(e -> saveImpl(e)).collect(Collectors.toList());
306296
}
307297

308298
// we need to determine the `isNew` state of the entities before calling the id generator
@@ -319,12 +309,11 @@ public <T> List<T> saveAll(Iterable<T> instances) {
319309
.collect(Collectors.toList());
320310
ResultSummary resultSummary = neo4jClient
321311
.query(() -> renderer.render(cypherGenerator.prepareSaveOfMultipleInstancesOf(entityMetaData)))
322-
.in(databaseName)
323312
.bind(entityList).to(Constants.NAME_OF_ENTITY_LIST_PARAM).run();
324313

325314
// Save related
326315
entitiesToBeSaved.forEach(entityToBeSaved -> processRelations(entityMetaData, entityToBeSaved,
327-
isNewIndicator.get(entitiesToBeSaved.indexOf(entityToBeSaved)), databaseName));
316+
isNewIndicator.get(entitiesToBeSaved.indexOf(entityToBeSaved))));
328317

329318
SummaryCounters counters = resultSummary.counters();
330319
log.debug(() -> String.format(
@@ -345,7 +334,7 @@ public <T> void deleteById(Object id, Class<T> domainType) {
345334
log.debug(() -> String.format("Deleting entity with id %s ", id));
346335

347336
Statement statement = cypherGenerator.prepareDeleteOf(entityMetaData, condition);
348-
ResultSummary summary = this.neo4jClient.query(renderer.render(statement)).in(getDatabaseName())
337+
ResultSummary summary = this.neo4jClient.query(renderer.render(statement))
349338
.bind(convertIdValues(entityMetaData.getRequiredIdProperty(), id))
350339
.to(nameOfParameter).run();
351340

@@ -389,8 +378,8 @@ public <T> void deleteAllById(Iterable<?> ids, Class<T> domainType) {
389378
log.debug(() -> String.format("Deleting all entities with the following ids: %s ", ids));
390379

391380
Statement statement = cypherGenerator.prepareDeleteOf(entityMetaData, condition);
392-
ResultSummary summary = this.neo4jClient.query(renderer.render(statement)).in(getDatabaseName()).bind(
393-
convertIdValues(entityMetaData.getRequiredIdProperty(), ids))
381+
ResultSummary summary = this.neo4jClient.query(renderer.render(statement))
382+
.bind(convertIdValues(entityMetaData.getRequiredIdProperty(), ids))
394383
.to(nameOfParameter).run();
395384

396385
log.debug(() -> String.format("Deleted %d nodes and %d relationships.", summary.counters().nodesDeleted(),
@@ -404,7 +393,7 @@ public void deleteAll(Class<?> domainType) {
404393
log.debug(() -> String.format("Deleting all nodes with primary label %s", entityMetaData.getPrimaryLabel()));
405394

406395
Statement statement = cypherGenerator.prepareDeleteOf(entityMetaData);
407-
ResultSummary summary = this.neo4jClient.query(renderer.render(statement)).in(getDatabaseName()).run();
396+
ResultSummary summary = this.neo4jClient.query(renderer.render(statement)).run();
408397

409398
log.debug(() -> String.format("Deleted %d nodes and %d relationships.", summary.counters().nodesDeleted(),
410399
summary.counters().relationshipsDeleted()));
@@ -434,14 +423,14 @@ private <T> ExecutableQuery<T> createExecutableQuery(Class<T> domainType, String
434423
}
435424

436425
private <T> T processRelations(Neo4jPersistentEntity<?> neo4jPersistentEntity, Object parentObject,
437-
boolean isParentObjectNew, @Nullable String inDatabase) {
426+
boolean isParentObjectNew) {
438427

439-
return processNestedRelations(neo4jPersistentEntity, parentObject, isParentObjectNew, inDatabase,
428+
return processNestedRelations(neo4jPersistentEntity, parentObject, isParentObjectNew,
440429
new NestedRelationshipProcessingStateMachine());
441430
}
442431

443432
private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Object parentObject,
444-
boolean isParentObjectNew, @Nullable String inDatabase, NestedRelationshipProcessingStateMachine stateMachine) {
433+
boolean isParentObjectNew, NestedRelationshipProcessingStateMachine stateMachine) {
445434

446435
PersistentPropertyAccessor<?> propertyAccessor = sourceEntity.getPropertyAccessor(parentObject);
447436
Object fromId = propertyAccessor.getProperty(sourceEntity.getRequiredIdProperty());
@@ -492,7 +481,7 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Obje
492481

493482
Statement relationshipRemoveQuery = cypherGenerator.prepareDeleteOf(sourceEntity, relationshipDescription);
494483

495-
neo4jClient.query(renderer.render(relationshipRemoveQuery)).in(inDatabase)
484+
neo4jClient.query(renderer.render(relationshipRemoveQuery))
496485
.bind(convertIdValues(sourceEntity.getIdProperty(), fromId)) //
497486
.to(Constants.FROM_ID_PARAMETER_NAME) //
498487
.bind(knownRelationshipsIds) //
@@ -518,12 +507,12 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Obje
518507
relatedNode = eventSupport.maybeCallBeforeBind(relatedNode);
519508

520509
Long relatedInternalId = saveRelatedNode(relatedNode, relationshipContext.getAssociationTargetType(),
521-
targetEntity, inDatabase);
510+
targetEntity);
522511

523512
CreateRelationshipStatementHolder statementHolder = neo4jMappingContext.createStatement(
524513
sourceEntity, relationshipContext, relatedValueToStore);
525514

526-
Optional<Long> relationshipInternalId = neo4jClient.query(renderer.render(statementHolder.getStatement())).in(inDatabase)
515+
Optional<Long> relationshipInternalId = neo4jClient.query(renderer.render(statementHolder.getStatement()))
527516
.bind(convertIdValues(sourceEntity.getRequiredIdProperty(), fromId)) //
528517
.to(Constants.FROM_ID_PARAMETER_NAME)
529518
.bind(relatedInternalId) //
@@ -543,7 +532,7 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Obje
543532
targetPropertyAccessor.setProperty(targetEntity.getRequiredIdProperty(), relatedInternalId);
544533
}
545534
if (processState != ProcessState.PROCESSED_ALL_VALUES) {
546-
processNestedRelations(targetEntity, targetPropertyAccessor.getBean(), isEntityNew, inDatabase, stateMachine);
535+
processNestedRelations(targetEntity, targetPropertyAccessor.getBean(), isEntityNew, stateMachine);
547536
}
548537
}
549538

@@ -553,14 +542,12 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Obje
553542
return (T) propertyAccessor.getBean();
554543
}
555544

556-
private <Y> Long saveRelatedNode(Object entity, Class<Y> entityType, NodeDescription targetNodeDescription,
557-
@Nullable String inDatabase) {
545+
private <Y> Long saveRelatedNode(Object entity, Class<Y> entityType, NodeDescription targetNodeDescription) {
558546

559-
DynamicLabels dynamicLabels = determineDynamicLabels(entity, (Neo4jPersistentEntity) targetNodeDescription,
560-
inDatabase);
547+
DynamicLabels dynamicLabels = determineDynamicLabels(entity, (Neo4jPersistentEntity) targetNodeDescription);
561548
Optional<Long> optionalSavedNodeId = neo4jClient
562549
.query(() -> renderer.render(cypherGenerator.prepareSaveOf(targetNodeDescription, dynamicLabels)))
563-
.in(inDatabase).bind((Y) entity).with(neo4jMappingContext.getRequiredBinderFunctionFor(entityType))
550+
.bind((Y) entity).with(neo4jMappingContext.getRequiredBinderFunctionFor(entityType))
564551
.fetchAs(Long.class).one();
565552

566553
if (((Neo4jPersistentEntity) targetNodeDescription).hasVersionProperty() && !optionalSavedNodeId.isPresent()) {
@@ -570,11 +557,6 @@ private <Y> Long saveRelatedNode(Object entity, Class<Y> entityType, NodeDescrip
570557
return optionalSavedNodeId.get();
571558
}
572559

573-
private String getDatabaseName() {
574-
575-
return this.databaseSelectionProvider.getDatabaseSelection().getValue();
576-
}
577-
578560
@Override
579561
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
580562

@@ -676,7 +658,7 @@ private Optional<Neo4jClient.RecordFetchSpec<T>> createFetchSpec() {
676658
}
677659

678660
Neo4jClient.MappingSpec<T> newMappingSpec = neo4jClient.query(cypherQuery)
679-
.in(getDatabaseName()).bindAll(finalParameters).fetchAs(preparedQuery.getResultType());
661+
.bindAll(finalParameters).fetchAs(preparedQuery.getResultType());
680662
return Optional.of(preparedQuery.getOptionalMappingFunction()
681663
.map(f -> newMappingSpec.mappedBy(f)).orElse(newMappingSpec));
682664
}
@@ -690,7 +672,7 @@ private GenericQueryAndParameters createQueryAndParameters(Neo4jPersistentEntity
690672
.returning(Constants.NAME_OF_SYNTHESIZED_ROOT_NODE).build();
691673

692674
final Collection<Long> rootNodeIds = new HashSet<>((Collection<Long>) neo4jClient
693-
.query(renderer.render(rootNodesStatement)).in(getDatabaseName())
675+
.query(renderer.render(rootNodesStatement))
694676
.bindAll(parameters)
695677
.fetch()
696678
.one()
@@ -716,7 +698,7 @@ private GenericQueryAndParameters createQueryAndParameters(Neo4jPersistentEntity
716698
.prepareMatchOf(entityMetaData, relationshipDescription, queryFragments.getMatchOn(), queryFragments.getCondition())
717699
.returning(cypherGenerator.createReturnStatementForMatch(entityMetaData)).build();
718700

719-
neo4jClient.query(renderer.render(statement)).in(getDatabaseName())
701+
neo4jClient.query(renderer.render(statement))
720702
.bindAll(parameters)
721703
.fetch()
722704
.one()
@@ -739,7 +721,7 @@ private void iterateNextLevel(Collection<Long> nodeIds, Neo4jPersistentEntity<?>
739721
Functions.id(node).in(Cypher.parameter(Constants.NAME_OF_IDS)))
740722
.returning(cypherGenerator.createGenericReturnStatement()).build();
741723

742-
neo4jClient.query(renderer.render(statement)).in(getDatabaseName())
724+
neo4jClient.query(renderer.render(statement))
743725
.bindAll(Collections.singletonMap(Constants.NAME_OF_IDS, nodeIds))
744726
.fetch()
745727
.one()

src/test/java/org/springframework/data/neo4j/integration/multiple_ctx_imperative/domain1/Domain1Config.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public Neo4jClient domain1Client(@Qualifier("domain1Driver") Driver driver) {
6363
@Primary @Bean
6464
public Neo4jOperations domain1Template(
6565
@Qualifier("domain1Client") Neo4jClient domain1Client,
66-
@Qualifier("domain1Context") Neo4jMappingContext domain1Context,
67-
@Qualifier("domain1Selection") DatabaseSelectionProvider domain1Selection
66+
@Qualifier("domain1Context") Neo4jMappingContext domain1Context
6867
) {
69-
return new Neo4jTemplate(domain1Client, domain1Context, domain1Selection);
68+
return new Neo4jTemplate(domain1Client, domain1Context);
7069
}
7170

7271
@Primary @Bean

src/test/java/org/springframework/data/neo4j/integration/multiple_ctx_imperative/domain2/Domain2Config.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ public Neo4jClient domain2Client(@Qualifier("domain2Driver") Driver driver) {
6262
@Bean
6363
public Neo4jOperations domain2Template(
6464
@Qualifier("domain2Client") Neo4jClient domain2Client,
65-
@Qualifier("domain2Context") Neo4jMappingContext domain2Context,
66-
@Qualifier("domain2Selection") DatabaseSelectionProvider domain2Selection
65+
@Qualifier("domain2Context") Neo4jMappingContext domain2Context
6766
) {
68-
return new Neo4jTemplate(domain2Client, domain2Context, domain2Selection);
67+
return new Neo4jTemplate(domain2Client, domain2Context);
6968
}
7069

7170
@Bean

0 commit comments

Comments
 (0)