GH-2828 - Use correct node name related node in find by example.

Closes #2828
This commit is contained in:
Gerrit Meier
2023-11-22 13:27:20 +01:00
parent d642e657dc
commit 4e66f793be
4 changed files with 27 additions and 2 deletions

View File

@@ -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;
@@ -93,6 +94,7 @@ public final class IdDescription {
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);
@@ -109,6 +111,23 @@ public final class IdDescription {
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);
}

View File

@@ -136,6 +136,9 @@ public interface NodeDescription<T> {
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() {

View File

@@ -167,7 +167,7 @@ final class Predicate {
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);

View File

@@ -3118,7 +3118,10 @@ class RepositoryIT {
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);