Skip to content

Commit

Permalink
GH-2828 - Use correct node name related node in find by example.
Browse files Browse the repository at this point in the history
Closes #2828
  • Loading branch information
meistermeier committed Nov 22, 2023
1 parent 3f2b627 commit 018d1d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class IdDescription {
* The property that stores the id if applicable.
*/
private @Nullable final String graphPropertyName;
private final boolean isDeprecated;

private final Lazy<Expression> idExpression;

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

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

/**
* Creates the right identifier expression for this node entity.
* Note: This enforces a recalculation of the name on invoke.
*
* @param nodeName use this name as the symbolic name of the node in the query
* @return An expression that represents the right identifier type.
*/
public Expression asIdExpression(String nodeName) {
final Node rootNode = Cypher.anyNode(nodeName);
if (this.isInternallyGeneratedId()) {
return isDeprecated ? Functions.id(rootNode) : Functions.elementId(rootNode);
} else {
return this.getOptionalGraphPropertyName()
.map(propertyName -> Cypher.property(nodeName, propertyName)).get();
}
}

public Optional<Class<? extends IdGenerator<?>>> getIdGeneratorClass() {
return Optional.ofNullable(idGeneratorClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ default boolean isUsingInternalIds() {
NodeDescription<?> getParentNodeDescription();

/**
* Creates the right identifier expression for this node entity.
* Note: The expression gets cached and won't get recalculated at every invocation.
*
* @return An expression that represents the right identifier type.
*/
default Expression getIdExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static void addConditionAndParameters(Neo4jMappingContext mappingContext
if (isRootNode) {
condition = predicate.neo4jPersistentEntity.getIdExpression().isEqualTo(literalOf(theValue));
} else {
condition = nodeDescription.getIdExpression().isEqualTo(literalOf(theValue));
condition = nodeDescription.getIdDescription().asIdExpression(wrapper.getNodeName()).isEqualTo(literalOf(theValue));
}
} else {
Expression property = !isRootNode ? property(wrapper.getNodeName(), propertyName) : property(Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription), propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,10 @@ void findEntityWithRelationshipByFindOneByExample(@Autowired RelationshipReposit
long petNode2Id = TestIdentitySupport.getInternalId(petNode2);

PersonWithRelationship probe = new PersonWithRelationship();
probe.setName("Freddie");
Hobby hobbies = new Hobby();
hobbies.setId(hobbyNodeId);
hobbies.setName("Music");
probe.setHobbies(hobbies);
PersonWithRelationship loadedPerson = repository.findOne(Example.of(probe)).get();
assertThat(loadedPerson.getName()).isEqualTo("Freddie");
assertThat(loadedPerson.getId()).isEqualTo(personId);
Expand Down

0 comments on commit 018d1d5

Please sign in to comment.