Skip to content

Commit bc1c23b

Browse files
GH-2289 - Don’t use the fromId with obverse to stop traversing relationships.
The previous check used the obverse combined with the fromId to check if traversal can be stopped or not. This is wrong, we would need to combine obverse with targetId, which we may not have at the moment. This change simplifies the check and only looks at visited relationships from source to somewhere else to avoid additional rounds. This fixes #2289.
1 parent f81fc92 commit bc1c23b

File tree

8 files changed

+337
-6
lines changed

8 files changed

+337
-6
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Pers
490490
rawValue);
491491

492492
RelationshipDescription relationshipDescription = relationshipContext.getRelationship();
493-
RelationshipDescription relationshipDescriptionObverse = relationshipDescription.getRelationshipObverse();
494493

495494
Neo4jPersistentProperty idProperty;
496495
if (!relationshipDescription.hasInternalIdProperty()) {
@@ -501,7 +500,7 @@ private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Pers
501500
}
502501

503502
// break recursive procession and deletion of previously created relationships
504-
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescriptionObverse, relatedValuesToStore);
503+
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescription, relatedValuesToStore);
505504
if (processState == ProcessState.PROCESSED_ALL_RELATIONSHIPS || processState == ProcessState.PROCESSED_BOTH) {
506505
return;
507506
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
606606
rawValue);
607607

608608
RelationshipDescription relationshipDescription = relationshipContext.getRelationship();
609-
RelationshipDescription relationshipDescriptionObverse = relationshipDescription.getRelationshipObverse();
610609

611610
Neo4jPersistentProperty idProperty;
612611
if (!relationshipDescription.hasInternalIdProperty()) {
@@ -617,7 +616,7 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
617616
}
618617

619618
// break recursive procession and deletion of previously created relationships
620-
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescriptionObverse, relatedValuesToStore);
619+
ProcessState processState = stateMachine.getStateOf(fromId, relationshipDescription, relatedValuesToStore);
621620
if (processState == ProcessState.PROCESSED_ALL_RELATIONSHIPS || processState == ProcessState.PROCESSED_BOTH) {
622621
return;
623622
}

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultRelationshipDescription.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public boolean equals(Object o) {
138138

139139
@Override
140140
public int hashCode() {
141-
return Objects.hash(type, type, fieldName, target, source, direction);
141+
return Objects.hash(fieldName, type, target, source, direction);
142142
}
143-
144143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2289;
17+
18+
import org.assertj.core.api.Assertions;
19+
import org.junit.jupiter.api.BeforeAll;
20+
import org.junit.jupiter.api.RepeatedTest;
21+
import org.neo4j.driver.Driver;
22+
import org.neo4j.driver.Session;
23+
import org.neo4j.driver.Transaction;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
28+
import org.springframework.data.neo4j.repository.Neo4jRepository;
29+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
30+
import org.springframework.data.neo4j.test.Neo4jExtension;
31+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
32+
import org.springframework.stereotype.Repository;
33+
import org.springframework.transaction.annotation.EnableTransactionManagement;
34+
35+
/**
36+
* @author Michael J. Simons
37+
*/
38+
@Neo4jIntegrationTest
39+
class GH2289IT {
40+
41+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
42+
43+
@BeforeAll
44+
protected static void setupData() {
45+
try (Session session = neo4jConnectionSupport.getDriver().session();
46+
Transaction transaction = session.beginTransaction();
47+
) {
48+
transaction.run("MATCH (n) detach delete n");
49+
transaction.commit();
50+
}
51+
}
52+
53+
@RepeatedTest(23)
54+
void testNewRelation(@Autowired SkuRepository skuRepo) {
55+
Sku a = skuRepo.save(new Sku(0L, "A"));
56+
Sku b = skuRepo.save(new Sku(1L, "B"));
57+
Sku c = skuRepo.save(new Sku(2L, "C"));
58+
Sku d = skuRepo.save(new Sku(3L, "D"));
59+
60+
a.rangeRelationTo(b, 1, 1, RelationType.MULTIPLICATIVE);
61+
a.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
62+
a.rangeRelationTo(d, 1, 1, RelationType.MULTIPLICATIVE);
63+
a = skuRepo.save(a);
64+
65+
Assertions.assertThat(a.getRangeRelationsOut()).hasSize(3);
66+
b = skuRepo.findById(b.getId()).get();
67+
Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1);
68+
69+
b.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
70+
b = skuRepo.save(b);
71+
Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1);
72+
Assertions.assertThat(b.getRangeRelationsOut()).hasSize(1);
73+
}
74+
75+
@Repository
76+
public interface SkuRepository extends Neo4jRepository<Sku, Long> {
77+
}
78+
79+
@Configuration
80+
@EnableTransactionManagement
81+
@EnableNeo4jRepositories(considerNestedRepositories = true)
82+
static class Config extends AbstractNeo4jConfig {
83+
84+
@Bean
85+
public Driver driver() {
86+
87+
return neo4jConnectionSupport.getDriver();
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2289;
17+
18+
import lombok.Data;
19+
20+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
21+
import org.springframework.data.neo4j.core.schema.Id;
22+
import org.springframework.data.neo4j.core.schema.Property;
23+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
24+
import org.springframework.data.neo4j.core.schema.TargetNode;
25+
26+
/**
27+
* @author Michael J. Simons
28+
*/
29+
@Data // lombok
30+
@RelationshipProperties
31+
public class RangeRelation {
32+
@Id @GeneratedValue private Long id;
33+
34+
@Property private double minDelta;
35+
@Property private double maxDelta;
36+
@Property private RelationType relationType;
37+
38+
@TargetNode private Sku targetSku;
39+
40+
public RangeRelation(Sku targetSku, double minDelta, double maxDelta, RelationType relationType) {
41+
this.targetSku = targetSku;
42+
this.minDelta = minDelta;
43+
this.maxDelta = maxDelta;
44+
this.relationType = relationType;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2289;
17+
18+
import reactor.test.StepVerifier;
19+
20+
import java.util.concurrent.atomic.AtomicLong;
21+
import java.util.concurrent.atomic.AtomicReference;
22+
23+
import org.assertj.core.api.Assertions;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.RepeatedTest;
26+
import org.junit.jupiter.api.Tag;
27+
import org.neo4j.driver.Driver;
28+
import org.neo4j.driver.Session;
29+
import org.neo4j.driver.Transaction;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig;
34+
import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;
35+
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
36+
import org.springframework.data.neo4j.test.Neo4jExtension;
37+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
38+
import org.springframework.stereotype.Repository;
39+
import org.springframework.transaction.annotation.EnableTransactionManagement;
40+
41+
/**
42+
* @author Michael J. Simons
43+
*/
44+
@Neo4jIntegrationTest
45+
@Tag(Neo4jExtension.NEEDS_REACTIVE_SUPPORT)
46+
class ReactiveGH2289IT {
47+
48+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
49+
50+
@BeforeAll
51+
protected static void setupData() {
52+
try (Session session = neo4jConnectionSupport.getDriver().session();
53+
Transaction transaction = session.beginTransaction();
54+
) {
55+
transaction.run("MATCH (n) detach delete n");
56+
transaction.commit();
57+
}
58+
}
59+
60+
@RepeatedTest(23)
61+
void testNewRelation(@Autowired SkuRepository skuRepo) {
62+
63+
AtomicLong bId = new AtomicLong();
64+
AtomicReference<Sku> cRef = new AtomicReference<>();
65+
skuRepo.save(new Sku(0L, "A"))
66+
.zipWith(skuRepo.save(new Sku(1L, "B")))
67+
.zipWith(skuRepo.save(new Sku(2L, "C")))
68+
.zipWith(skuRepo.save(new Sku(3L, "D"))).flatMap(t -> {
69+
Sku a = t.getT1().getT1().getT1();
70+
Sku b = t.getT1().getT1().getT2();
71+
Sku c = t.getT1().getT2();
72+
Sku d = t.getT2();
73+
74+
bId.set(b.getId());
75+
cRef.set(c);
76+
a.rangeRelationTo(b, 1, 1, RelationType.MULTIPLICATIVE);
77+
a.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE);
78+
a.rangeRelationTo(d, 1, 1, RelationType.MULTIPLICATIVE);
79+
return skuRepo.save(a);
80+
}).as(StepVerifier::create)
81+
.expectNextMatches(a -> a.getRangeRelationsOut().size() == 3)
82+
.verifyComplete();
83+
84+
skuRepo.findById(bId.get())
85+
.doOnNext(b -> Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1))
86+
.flatMap(b -> {
87+
b.rangeRelationTo(cRef.get(), 1, 1, RelationType.MULTIPLICATIVE);
88+
return skuRepo.save(b);
89+
})
90+
.as(StepVerifier::create)
91+
.expectNextMatches(a -> a.getRangeRelationsIn().size() == 1 && a.getRangeRelationsOut().size() == 1)
92+
.verifyComplete();
93+
}
94+
95+
@Repository
96+
public interface SkuRepository extends ReactiveNeo4jRepository<Sku, Long> {
97+
}
98+
99+
@Configuration
100+
@EnableTransactionManagement
101+
@EnableReactiveNeo4jRepositories(considerNestedRepositories = true)
102+
static class Config extends AbstractReactiveNeo4jConfig {
103+
104+
@Bean
105+
public Driver driver() {
106+
107+
return neo4jConnectionSupport.getDriver();
108+
}
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2289;
17+
18+
/**
19+
* @author Michael J. Simons
20+
*/
21+
public enum RelationType {
22+
MULTIPLICATIVE
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2289;
17+
18+
import lombok.Getter;
19+
import lombok.Setter;
20+
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
25+
import org.springframework.data.neo4j.core.schema.Id;
26+
import org.springframework.data.neo4j.core.schema.Node;
27+
import org.springframework.data.neo4j.core.schema.Property;
28+
import org.springframework.data.neo4j.core.schema.Relationship;
29+
30+
/**
31+
* @author Michael J. Simons
32+
*/
33+
@Node("SKU")
34+
@Getter // lombok
35+
@Setter
36+
public class Sku {
37+
38+
@Id @GeneratedValue
39+
private Long id;
40+
41+
@Property("number")
42+
private Long number;
43+
44+
@Property("name")
45+
private String name;
46+
47+
@Relationship(type = "RANGE_RELATION_TO", direction = Relationship.Direction.OUTGOING)
48+
private Set<RangeRelation> rangeRelationsOut = new HashSet<>();
49+
50+
@Relationship(type = "RANGE_RELATION_TO", direction = Relationship.Direction.INCOMING)
51+
private Set<RangeRelation> rangeRelationsIn = new HashSet<>();
52+
53+
public Sku(Long number, String name) {
54+
this.number = number;
55+
this.name = name;
56+
}
57+
58+
public RangeRelation rangeRelationTo(Sku sku, double minDelta, double maxDelta, RelationType relationType) {
59+
RangeRelation relationOut = new RangeRelation(sku, minDelta, maxDelta, relationType);
60+
RangeRelation relationIn = new RangeRelation(this, minDelta, maxDelta, relationType);
61+
rangeRelationsOut.add(relationOut);
62+
sku.rangeRelationsIn.add(relationIn);
63+
return relationOut;
64+
}
65+
}

0 commit comments

Comments
 (0)