Skip to content

Commit d192a80

Browse files
GH-2638 - Use labels(n) when deriving a contains query for an entity with dynamic labels.
Closes #2638.
1 parent 7dce1b7 commit d192a80

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/main/java/org/springframework/data/neo4j/repository/query/CypherQueryCreator.java

+8
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ private Condition containingCondition(PersistentPropertyPath<Neo4jPersistentProp
382382
Neo4jPersistentProperty property, Iterator<Object> actualParameters, boolean ignoreCase) {
383383

384384
Expression cypherProperty = toCypherProperty(path, ignoreCase);
385+
386+
if (property.isDynamicLabels()) {
387+
Neo4jPersistentProperty leafProperty = path.getRequiredLeafProperty();
388+
Neo4jPersistentEntity<?> owner = (Neo4jPersistentEntity<?>) leafProperty.getOwner();
389+
String containerName = getContainerName(path, owner);
390+
return toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase)
391+
.in(Functions.labels(Cypher.anyNode(containerName)));
392+
}
385393
if (property.isCollectionLike()) {
386394
return toCypherParameter(nextRequiredParameter(actualParameters, property), ignoreCase).in(cypherProperty);
387395
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.beans.factory.annotation.Autowired;
4343
import org.springframework.context.annotation.Bean;
4444
import org.springframework.context.annotation.Configuration;
45+
import org.springframework.data.neo4j.integration.shared.common.Port;
4546
import org.springframework.data.neo4j.repository.Neo4jRepository;
4647
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
4748
import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration;
@@ -446,6 +447,30 @@ void instantiateConcreteEntityType(@Autowired AbstractBaseEntityWithDynamicLabel
446447

447448
}
448449

450+
@Nested
451+
class FindByLabelsContaining extends SpringTestBase {
452+
453+
@Override
454+
Long createTestEntity(TransactionContext t) {
455+
t.run("CREATE (p:Port:A:B {id: randomUUID()})");
456+
t.run("CREATE (p:Port {id: randomUUID()})");
457+
t.run("CREATE (p:Port:C:B {id: randomUUID()})");
458+
t.run("CREATE (p:Port:D:A {id: randomUUID()})");
459+
return null;
460+
}
461+
462+
@Test // GH-2638
463+
void findByDynamicLabelsContainingShouldWork(@Autowired PortRepository portRepository) {
464+
465+
List<Port> ports = portRepository.findByLabelsContaining("A");
466+
assertThat(ports).hasSize(2);
467+
}
468+
}
469+
470+
interface PortRepository extends Neo4jRepository<Port, UUID> {
471+
List<Port> findByLabelsContaining(String label);
472+
}
473+
449474
interface AbstractBaseEntityWithDynamicLabelsRepository extends Neo4jRepository<EntitiesWithDynamicLabels.AbstractBaseEntityWithDynamicLabels, String> {}
450475

451476
@ExtendWith(SpringExtension.class)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2011-2022 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.shared.common;
17+
18+
import java.util.List;
19+
import java.util.UUID;
20+
21+
import org.springframework.data.neo4j.core.schema.DynamicLabels;
22+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
23+
import org.springframework.data.neo4j.core.schema.Id;
24+
import org.springframework.data.neo4j.core.schema.Node;
25+
26+
/**
27+
* @author Michael J. Simons
28+
*/
29+
@Node("Port")
30+
public class Port {
31+
@Id
32+
@GeneratedValue
33+
private UUID id;
34+
private String code;
35+
@DynamicLabels
36+
private List<String> labels;
37+
}

0 commit comments

Comments
 (0)