Skip to content

Commit 018d1d5

Browse files
committed
GH-2828 - Use correct node name related node in find by example.
Closes #2828
1 parent 3f2b627 commit 018d1d5

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class IdDescription {
5252
* The property that stores the id if applicable.
5353
*/
5454
private @Nullable final String graphPropertyName;
55+
private final boolean isDeprecated;
5556

5657
private final Lazy<Expression> idExpression;
5758

@@ -93,6 +94,7 @@ private IdDescription(SymbolicName symbolicName, @Nullable Class<? extends IdGen
9394
this.idGeneratorClass = idGeneratorClass;
9495
this.idGeneratorRef = idGeneratorRef != null && idGeneratorRef.isEmpty() ? null : idGeneratorRef;
9596
this.graphPropertyName = graphPropertyName;
97+
this.isDeprecated = isDeprecated;
9698

9799
this.idExpression = Lazy.of(() -> {
98100
final Node rootNode = Cypher.anyNode(symbolicName);
@@ -109,6 +111,23 @@ public Expression asIdExpression() {
109111
return this.idExpression.get();
110112
}
111113

114+
/**
115+
* Creates the right identifier expression for this node entity.
116+
* Note: This enforces a recalculation of the name on invoke.
117+
*
118+
* @param nodeName use this name as the symbolic name of the node in the query
119+
* @return An expression that represents the right identifier type.
120+
*/
121+
public Expression asIdExpression(String nodeName) {
122+
final Node rootNode = Cypher.anyNode(nodeName);
123+
if (this.isInternallyGeneratedId()) {
124+
return isDeprecated ? Functions.id(rootNode) : Functions.elementId(rootNode);
125+
} else {
126+
return this.getOptionalGraphPropertyName()
127+
.map(propertyName -> Cypher.property(nodeName, propertyName)).get();
128+
}
129+
}
130+
112131
public Optional<Class<? extends IdGenerator<?>>> getIdGeneratorClass() {
113132
return Optional.ofNullable(idGeneratorClass);
114133
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ default boolean isUsingInternalIds() {
136136
NodeDescription<?> getParentNodeDescription();
137137

138138
/**
139+
* Creates the right identifier expression for this node entity.
140+
* Note: The expression gets cached and won't get recalculated at every invocation.
141+
*
139142
* @return An expression that represents the right identifier type.
140143
*/
141144
default Expression getIdExpression() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private static void addConditionAndParameters(Neo4jMappingContext mappingContext
167167
if (isRootNode) {
168168
condition = predicate.neo4jPersistentEntity.getIdExpression().isEqualTo(literalOf(theValue));
169169
} else {
170-
condition = nodeDescription.getIdExpression().isEqualTo(literalOf(theValue));
170+
condition = nodeDescription.getIdDescription().asIdExpression(wrapper.getNodeName()).isEqualTo(literalOf(theValue));
171171
}
172172
} else {
173173
Expression property = !isRootNode ? property(wrapper.getNodeName(), propertyName) : property(Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription), propertyName);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3118,7 +3118,10 @@ void findEntityWithRelationshipByFindOneByExample(@Autowired RelationshipReposit
31183118
long petNode2Id = TestIdentitySupport.getInternalId(petNode2);
31193119

31203120
PersonWithRelationship probe = new PersonWithRelationship();
3121-
probe.setName("Freddie");
3121+
Hobby hobbies = new Hobby();
3122+
hobbies.setId(hobbyNodeId);
3123+
hobbies.setName("Music");
3124+
probe.setHobbies(hobbies);
31223125
PersonWithRelationship loadedPerson = repository.findOne(Example.of(probe)).get();
31233126
assertThat(loadedPerson.getName()).isEqualTo("Freddie");
31243127
assertThat(loadedPerson.getId()).isEqualTo(personId);

0 commit comments

Comments
 (0)