Skip to content

Commit a0adf09

Browse files
committed
GH-2223 - Compile fix and test case.
1 parent 64a2e3e commit a0adf09

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private <T> T processRelations(Neo4jPersistentEntity<?> neo4jPersistentEntity, T
456456
PersistentPropertyAccessor<?> parentPropertyAccessor,
457457
@Nullable String inDatabase, boolean isParentObjectNew) {
458458

459-
return processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew, inDatabase
459+
return processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew, inDatabase,
460460
new NestedRelationshipProcessingStateMachine(originalInstance));
461461
}
462462

@@ -547,8 +547,7 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Pers
547547
Object thing = stateMachine.getProcessedAs(newRelatedObject);
548548
relatedInternalId = queryRelatedNode(thing, targetEntity, inDatabase);
549549
} else {
550-
relatedInternalId = saveRelatedNode(newRelatedObject, relationshipContext.getAssociationTargetType(),
551-
targetEntity, inDatabase);
550+
relatedInternalId = saveRelatedNode(newRelatedObject, targetEntity, inDatabase);
552551
}
553552
stateMachine.markValueAsProcessed(relatedValueToStore);
554553

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private <T> Mono<T> saveImpl(T instance, @Nullable String inDatabase) {
256256
if (entityMetaData.isUsingInternalIds()) {
257257
propertyAccessor.setProperty(entityMetaData.getRequiredIdProperty(), internalId);
258258
}
259-
}).then(Mono.defer(() -> processRelations(entityMetaData, instance, propertyAccessor, isNewEntity, inDatabase)));
259+
}).then(Mono.defer(() -> processRelations(entityMetaData, instance, propertyAccessor, inDatabase, isNewEntity)));
260260
});
261261
}
262262

@@ -333,7 +333,7 @@ public <T> Flux<T> saveAll(Iterable<T> instances) {
333333
counters.nodesCreated(), counters.nodesDeleted(), counters.relationshipsCreated(),
334334
counters.relationshipsDeleted(), counters.propertiesSet()));
335335
}).thenMany(Flux.fromIterable(entitiesToBeSaved)
336-
.flatMap(t -> processRelations(entityMetaData, t.getT1(), entityMetaData.getPropertyAccessor(t.getT3()), t.getT2()))
336+
.flatMap(t -> processRelations(entityMetaData, t.getT1(), entityMetaData.getPropertyAccessor(t.getT3()), databaseName.getValue(), t.getT2()))
337337
)));
338338
}
339339

@@ -574,7 +574,7 @@ private <T> Mono<T> processRelations(Neo4jPersistentEntity<?> neo4jPersistentEnt
574574
PersistentPropertyAccessor<?> parentPropertyAccessor,
575575
@Nullable String inDatabase, boolean isParentObjectNew) {
576576

577-
return processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew, inDatabase
577+
return processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew, inDatabase,
578578
new NestedRelationshipProcessingStateMachine(originalInstance));
579579
}
580580

@@ -663,7 +663,7 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
663663
if (stateMachine.hasProcessedValue(relatedValueToStore)) {
664664
queryOrSave = queryRelatedNode(newRelatedObject, targetEntity, inDatabase);
665665
} else {
666-
queryOrSave = saveRelatedNode(newRelatedObject, relationshipContext.getAssociationTargetType(), targetEntity, inDatabase);
666+
queryOrSave = saveRelatedNode(newRelatedObject, targetEntity, inDatabase);
667667
}
668668
stateMachine.markValueAsProcessed(relatedValueToStore);
669669
return queryOrSave.flatMap(relatedInternalId -> {

src/test/java/org/springframework/data/neo4j/integration/imperative/ImmutableGeneratedIdsIT.java

+49-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.imperative;
1717

18+
import org.junit.jupiter.api.BeforeEach;
1819
import org.junit.jupiter.api.Test;
1920
import org.neo4j.driver.Driver;
21+
import org.neo4j.driver.Record;
22+
import org.neo4j.driver.Session;
2023
import org.springframework.beans.factory.annotation.Autowired;
2124
import org.springframework.context.annotation.Bean;
2225
import org.springframework.context.annotation.Configuration;
@@ -36,6 +39,7 @@
3639
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
3740
import org.springframework.transaction.annotation.EnableTransactionManagement;
3841

42+
import java.util.ArrayList;
3943
import java.util.Arrays;
4044
import java.util.Collection;
4145
import java.util.Collections;
@@ -54,6 +58,19 @@ public class ImmutableGeneratedIdsIT {
5458

5559
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
5660

61+
private final Driver driver;
62+
63+
public ImmutableGeneratedIdsIT(@Autowired Driver driver) {
64+
this.driver = driver;
65+
}
66+
67+
@BeforeEach
68+
void cleanUp() {
69+
try (Session session = driver.session()) {
70+
session.run("MATCH (n) DETACH DELETE n").consume();
71+
}
72+
}
73+
5774
@Test // GH-2141
5875
void saveWithGeneratedIdsReturnsObjectWithIdSet(
5976
@Autowired ImmutablePersonWithGeneratedIdRepository repository) {
@@ -279,7 +296,6 @@ void saveRelationshipWithGeneratedIdsContainsAllRelationshipTypes(
279296
assertThat(savedPerson.relationshipPropertiesDynamicCollection.values().iterator().next().get(0).target.id).isNotNull();
280297
}
281298

282-
interface ImmutablePersonWithGeneratedIdRepository extends Neo4jRepository<ImmutablePersonWithGeneratedId, Long> {}
283299

284300
@Test // GH-2148
285301
void childrenShouldNotBeRecreatedForNoReasons(@Autowired Neo4jTemplate template) {
@@ -295,6 +311,38 @@ void childrenShouldNotBeRecreatedForNoReasons(@Autowired Neo4jTemplate template)
295311
assertThat(saved.getChildren()).allMatch(c -> c.getId() != null && children.contains(c));
296312
}
297313

314+
@Test // GH-2223
315+
void saveWithGeneratedIdsWithMultipleRelationshipsToOneNode(
316+
@Autowired ImmutablePersonWithGeneratedIdRepository repository) {
317+
318+
ImmutablePersonWithGeneratedId person1 = new ImmutablePersonWithGeneratedId();
319+
ImmutablePersonWithGeneratedId person2 = ImmutablePersonWithGeneratedId.fallback(person1);
320+
List<ImmutablePersonWithGeneratedId> onboardedBy = new ArrayList<>();
321+
onboardedBy.add(person1);
322+
onboardedBy.add(person2);
323+
ImmutablePersonWithGeneratedId person3 = ImmutablePersonWithGeneratedId.wasOnboardedBy(onboardedBy);
324+
325+
ImmutablePersonWithGeneratedId savedPerson = repository.save(person3);
326+
assertThat(savedPerson.id).isNotNull();
327+
assertThat(savedPerson.wasOnboardedBy).allMatch(ob -> ob.id != null);
328+
329+
ImmutablePersonWithGeneratedId savedPerson2 = savedPerson.wasOnboardedBy.stream().filter(p -> p.fallback != null).findFirst().get();
330+
assertThat(savedPerson2.fallback.id).isNotNull();
331+
332+
try (Session session = driver.session()) {
333+
List<Record> result = session.run(
334+
"MATCH (person3:ImmutablePersonWithGeneratedId) " +
335+
"-[:ONBOARDED_BY]->(person2:ImmutablePersonWithGeneratedId) " +
336+
"-[:FALLBACK]->(person1:ImmutablePersonWithGeneratedId), " +
337+
"(person3)-[:ONBOARDED_BY]->(person1) " +
338+
"return person3")
339+
.list();
340+
assertThat(result).hasSize(1);
341+
}
342+
}
343+
344+
interface ImmutablePersonWithGeneratedIdRepository extends Neo4jRepository<ImmutablePersonWithGeneratedId, Long> {}
345+
298346
@Configuration
299347
@EnableNeo4jRepositories(considerNestedRepositories = true)
300348
@EnableTransactionManagement

src/test/java/org/springframework/data/neo4j/integration/imperative/RepositoryIT.java

-26
Original file line numberDiff line numberDiff line change
@@ -1883,32 +1883,6 @@ void saveAllWithConvertedId(@Autowired EntityWithConvertedIdRepository repositor
18831883
assertThat(node.get("e").get("identifyingEnum").asString()).isEqualTo("A");
18841884
}
18851885
}
1886-
1887-
@Test // DATAGRAPH-1452
1888-
void createWithCustomQueryShouldWorkWithPlainObjects(@Autowired PersonRepository repository) {
1889-
1890-
PersonWithAllConstructor p = new PersonWithAllConstructor(null, "NewName", "NewFirstName", null, null, null, LocalDate.now(), null, null, null, null);
1891-
1892-
PersonWithAllConstructor newPerson = repository.createWithCustomQuery(p);
1893-
assertThat(newPerson.getName()).isEqualTo(p.getName());
1894-
assertThat(newPerson.getFirstName()).isEqualTo(p.getFirstName());
1895-
assertThat(newPerson.getBornOn()).isEqualTo(p.getBornOn());
1896-
}
1897-
1898-
@Test // GH-2141
1899-
void saveWithGeneratedIdsReturnsObjectWithIdSet(
1900-
@Autowired ImmutablePersonWithGeneratedIdRepository repository) {
1901-
1902-
ImmutablePersonWithGeneratedId fallback1 = new ImmutablePersonWithGeneratedId();
1903-
ImmutablePersonWithGeneratedId fallback2 = new ImmutablePersonWithGeneratedId(fallback1);
1904-
ImmutablePersonWithGeneratedId person = new ImmutablePersonWithGeneratedId(fallback2);
1905-
1906-
ImmutablePersonWithGeneratedId savedPerson = repository.save(person);
1907-
1908-
assertThat(savedPerson.getId()).isNotNull();
1909-
assertThat(savedPerson.getFallback()).isNotNull();
1910-
assertThat(savedPerson.getFallback().getFallback()).isNotNull();
1911-
}
19121886
}
19131887

19141888
@Nested

0 commit comments

Comments
 (0)