Skip to content

Commit 4ff3e11

Browse files
fix: Look for static labels to contain a label, not vice versa. (#2887)
Fixes #2886.
1 parent b2744f0 commit 4ff3e11

File tree

8 files changed

+308
-4
lines changed

8 files changed

+308
-4
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,11 @@ private boolean calculatePossibleCircles(NodeDescription<?> nodeDescription, Set
608608
visitedNodes.addAll(visitedTargetNodes);
609609
return false;
610610
}
611+
612+
@Override
613+
public String toString() {
614+
return "DefaultNeo4jPersistentEntity{" +
615+
"primaryLabel='" + primaryLabel + '\'' +
616+
'}';
617+
}
611618
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public NodeDescriptionAndLabels deriveConcreteNodeDescription(NodeDescription<?>
102102
return nodeDescriptionAndLabels.apply(entityDescription, labels);
103103
}
104104

105-
private NodeDescriptionAndLabels computeConcreteNodeDescription(NodeDescription<?> entityDescription, List<String> labels) {
105+
private NodeDescriptionAndLabels computeConcreteNodeDescription(NodeDescription<?> entityDescription, @Nullable List<String> labels) {
106106

107107
boolean isConcreteClassThatFulfillsEverything = !Modifier.isAbstract(entityDescription.getUnderlyingClass().getModifiers()) && entityDescription.getStaticLabels().containsAll(labels);
108108

@@ -139,9 +139,9 @@ private NodeDescriptionAndLabels computeConcreteNodeDescription(NodeDescription<
139139

140140
int unmatchedLabelsCount = 0;
141141
List<String> matchingLabels = new ArrayList<>();
142-
for (String staticLabel : staticLabels) {
143-
if (labels.contains(staticLabel)) {
144-
matchingLabels.add(staticLabel);
142+
for (String label : labels) {
143+
if (staticLabels.contains(label)) {
144+
matchingLabels.add(label);
145145
} else {
146146
unmatchedLabelsCount++;
147147
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import org.springframework.data.neo4j.core.schema.Node;
19+
20+
/**
21+
* GH-2886
22+
*/
23+
@Node(primaryLabel = "Apple")
24+
public class Apple extends MagicalFruit {
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
import org.springframework.data.neo4j.core.schema.DynamicLabels;
22+
import org.springframework.data.neo4j.core.schema.Id;
23+
import org.springframework.data.neo4j.core.schema.Node;
24+
25+
/**
26+
* GH-2886
27+
*/
28+
@Node(primaryLabel = "Fruit")
29+
public abstract class Fruit {
30+
31+
@Id
32+
protected String id;
33+
34+
@DynamicLabels
35+
protected Set<String> labels = new HashSet<>();
36+
37+
public String getId() {
38+
return this.id;
39+
}
40+
41+
public Set<String> getLabels() {
42+
return this.labels;
43+
}
44+
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public void setLabels(Set<String> labels) {
50+
this.labels = labels;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.neo4j.repository.Neo4jRepository;
21+
import org.springframework.data.neo4j.repository.query.Query;
22+
import org.springframework.stereotype.Repository;
23+
24+
/**
25+
* GH-2886
26+
*/
27+
@Repository
28+
public interface FruitRepository extends Neo4jRepository<Fruit, String> {
29+
@Query("MATCH (f:Fruit) RETURN f")
30+
List<Fruit> findAllFruits();
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
25+
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
import org.neo4j.driver.Driver;
29+
import org.neo4j.driver.Session;
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.core.DatabaseSelectionProvider;
34+
import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager;
35+
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
36+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
37+
import org.springframework.data.neo4j.test.BookmarkCapture;
38+
import org.springframework.data.neo4j.test.Neo4jExtension;
39+
import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration;
40+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
41+
import org.springframework.transaction.PlatformTransactionManager;
42+
import org.springframework.transaction.annotation.EnableTransactionManagement;
43+
44+
/**
45+
* GH-2886
46+
*/
47+
@Neo4jIntegrationTest
48+
public class Gh2886IT {
49+
50+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
51+
52+
@BeforeAll
53+
static void setup(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture) {
54+
try (Session session = driver.session(bookmarkCapture.createSessionConfig())) {
55+
session.run("MATCH (n) detach delete n").consume();
56+
bookmarkCapture.seedWith(session.lastBookmark());
57+
}
58+
}
59+
60+
@Test
61+
void dynamicLabels(@Autowired FruitRepository repository) {
62+
63+
Apple f1 = new Apple();
64+
f1.setVolume(1.0);
65+
f1.setColor("Red");
66+
f1.setLabels(Collections.singleton("X"));
67+
68+
Apple f2 = new Apple();
69+
f2.setColor("Blue");
70+
71+
Orange f3 = new Orange();
72+
f2.setVolume(3.0);
73+
f3.setColor("Red");
74+
f3.setLabels(Collections.singleton("Y"));
75+
76+
Orange f4 = new Orange();
77+
f4.setColor("Yellow");
78+
79+
repository.saveAll(Stream.of(f1, f2, f3, f4).collect(Collectors.toList()));
80+
81+
List<Fruit> fruits = repository.findAllFruits();
82+
assertThat(fruits).allMatch(f -> f instanceof Apple || f instanceof Orange);
83+
}
84+
85+
@Configuration
86+
@EnableTransactionManagement
87+
@EnableNeo4jRepositories(considerNestedRepositories = true)
88+
static class Config extends Neo4jImperativeTestConfiguration {
89+
90+
@Bean
91+
public BookmarkCapture bookmarkCapture() {
92+
return new BookmarkCapture();
93+
}
94+
95+
@Override
96+
public PlatformTransactionManager transactionManager(
97+
Driver driver, DatabaseSelectionProvider databaseNameProvider) {
98+
99+
BookmarkCapture bookmarkCapture = bookmarkCapture();
100+
return new Neo4jTransactionManager(driver, databaseNameProvider,
101+
Neo4jBookmarkManager.create(bookmarkCapture));
102+
}
103+
104+
@Override
105+
protected Collection<String> getMappingBasePackages() {
106+
return Collections.singleton(Apple.class.getPackage().getName());
107+
}
108+
109+
@Bean
110+
public Driver driver() {
111+
return neo4jConnectionSupport.getDriver();
112+
}
113+
114+
@Override
115+
public boolean isCypher5Compatible() {
116+
return neo4jConnectionSupport.isCypher5SyntaxCompatible();
117+
}
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import org.springframework.data.neo4j.core.schema.Node;
19+
20+
/**
21+
* GH-2886
22+
*/
23+
@Node(primaryLabel = "MagicalFruit")
24+
public class MagicalFruit extends Fruit {
25+
26+
private double volume;
27+
28+
private String color;
29+
30+
public double getVolume() {
31+
return this.volume;
32+
}
33+
34+
public String getColor() {
35+
return this.color;
36+
}
37+
38+
public void setVolume(double volume) {
39+
this.volume = volume;
40+
}
41+
42+
public void setColor(String color) {
43+
this.color = color;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2011-2024 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.gh2886;
17+
18+
import org.springframework.data.neo4j.core.schema.Node;
19+
20+
/**
21+
* GH-2886
22+
*/
23+
@Node(primaryLabel = "Orange")
24+
public class Orange extends MagicalFruit {
25+
}

0 commit comments

Comments
 (0)