Skip to content

Commit a39d6b2

Browse files
committed
GH-2462 - Cleaning up.
1 parent febb7fc commit a39d6b2

File tree

3 files changed

+14
-36
lines changed

3 files changed

+14
-36
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ private MapProjection projectPropertiesAndRelationships(PropertyFilter.RelaxedPr
550550
List<Object> propertiesProjection = projectNodeProperties(parentPath, nodeDescription, nodeName, relationshipDescription, includedProperties);
551551
List<Object> contentOfProjection = new ArrayList<>(propertiesProjection);
552552

553-
contentOfProjection.addAll(generateListsFor(parentPath, relationships, nodeName, includedProperties, processedRelationships));
553+
contentOfProjection.addAll(generateListsFor(parentPath, nodeDescription, relationships, nodeName, includedProperties, processedRelationships));
554554
return Cypher.anyNode(nodeName).project(contentOfProjection);
555555
}
556556

@@ -612,7 +612,7 @@ private List<Object> projectNodeProperties(PropertyFilter.RelaxedPropertyPath pa
612612
/**
613613
* @see CypherGenerator#projectNodeProperties
614614
*/
615-
private List<Object> generateListsFor(PropertyFilter.RelaxedPropertyPath parentPath, Collection<RelationshipDescription> relationships, SymbolicName nodeName,
615+
private List<Object> generateListsFor(PropertyFilter.RelaxedPropertyPath parentPath, Neo4jPersistentEntity<?> nodeDescription, Collection<RelationshipDescription> relationships, SymbolicName nodeName,
616616
Predicate<PropertyFilter.RelaxedPropertyPath> includedProperties, List<RelationshipDescription> processedRelationships) {
617617

618618
List<Object> mapProjectionLists = new ArrayList<>();
@@ -628,17 +628,17 @@ private List<Object> generateListsFor(PropertyFilter.RelaxedPropertyPath parentP
628628
continue;
629629
}
630630

631-
generateListFor(parentPath, relationshipDescription, nodeName, processedRelationships, fieldName, mapProjectionLists, includedProperties);
631+
generateListFor(parentPath, nodeDescription, relationshipDescription, nodeName, processedRelationships, fieldName, mapProjectionLists, includedProperties);
632632
}
633633

634634
return mapProjectionLists;
635635
}
636636

637-
private void generateListFor(PropertyFilter.RelaxedPropertyPath parentPath, RelationshipDescription relationshipDescription, SymbolicName nodeName,
637+
private void generateListFor(PropertyFilter.RelaxedPropertyPath parentPath, Neo4jPersistentEntity<?> nodeDescription, RelationshipDescription relationshipDescription, SymbolicName nodeName,
638638
List<RelationshipDescription> processedRelationships, String fieldName, List<Object> mapProjectionLists, Predicate<PropertyFilter.RelaxedPropertyPath> includedProperties) {
639639

640640
String relationshipType = relationshipDescription.getType();
641-
String relationshipTargetName = relationshipDescription.generateRelatedNodesCollectionName(relationshipDescription.getSource());
641+
String relationshipTargetName = relationshipDescription.generateRelatedNodesCollectionName(nodeDescription);
642642
String sourcePrimaryLabel = relationshipDescription.getSource().getMostAbstractParentLabel(relationshipDescription.getSource());
643643
String targetPrimaryLabel = relationshipDescription.getTarget().getPrimaryLabel();
644644
List<String> targetAdditionalLabels = relationshipDescription.getTarget().getAdditionalLabels();

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -342,24 +342,8 @@ private <ET> void populateProperties(MapAccessor queryResult, Neo4jPersistentEnt
342342
// store unless we temporarily put it there.
343343
knownObjects.storeObject(internalId, mappedObject);
344344

345-
// Fill associations
346-
// If the nodeDescription is a more abstract class as the concrete node description,
347-
// it might contain associations not named CONCRETE_TYPE_TARGET but ABSTRACT_TYPE_TARGET.
348-
// Those won't get caught by the most concrete node description.
349-
if (nodeDescription != concreteNodeDescription) {
350-
nodeDescription.doWithAssociations(
351-
populateFrom(queryResult, propertyAccessor, isConstructorParameter, objectAlreadyMapped, relationshipsFromResult, nodesFromResult));
352-
353-
// check intermediate types
354-
NodeDescription<?> parentNodeDescription = concreteNodeDescription.getParentNodeDescription();
355-
while (parentNodeDescription != null && parentNodeDescription != concreteNodeDescription) {
356-
((Neo4jPersistentEntity<?>) parentNodeDescription).doWithAssociations(
357-
populateFrom(queryResult, propertyAccessor, isConstructorParameter, objectAlreadyMapped, relationshipsFromResult, nodesFromResult));
358-
parentNodeDescription = parentNodeDescription.getParentNodeDescription();
359-
}
360-
}
361345
concreteNodeDescription.doWithAssociations(
362-
populateFrom(queryResult, propertyAccessor, isConstructorParameter, objectAlreadyMapped, relationshipsFromResult, nodesFromResult));
346+
populateFrom(queryResult, nodeDescription, propertyAccessor, isConstructorParameter, objectAlreadyMapped, relationshipsFromResult, nodesFromResult));
363347
}
364348

365349
@Nullable
@@ -440,12 +424,12 @@ public <T> T getParameterValue(PreferredConstructor.Parameter<T, Neo4jPersistent
440424
// If we cannot find any value it does not mean that there isn't any.
441425
// The result set might contain associations not named CONCRETE_TYPE_TARGET but ABSTRACT_TYPE_TARGET.
442426
// For this we bubble up the hierarchy of NodeDescriptions.
443-
result = createInstanceOfRelationships(matchingProperty, values, relationshipDescription, relationshipsFromResult, nodesFromResult, null)
427+
result = createInstanceOfRelationships(matchingProperty, values, relationshipDescription, nodeDescription, relationshipsFromResult, nodesFromResult)
444428
.orElseGet(() -> {
445429
NodeDescription<?> parentNodeDescription = nodeDescription.getParentNodeDescription();
446430
T resultValue = null;
447431
while (parentNodeDescription != null) {
448-
Optional<Object> value = createInstanceOfRelationships(matchingProperty, values, relationshipDescription, relationshipsFromResult, nodesFromResult, parentNodeDescription);
432+
Optional<Object> value = createInstanceOfRelationships(matchingProperty, values, relationshipDescription, parentNodeDescription, relationshipsFromResult, nodesFromResult);
449433
if (value.isPresent()) {
450434
resultValue = (T) value.get();
451435
break;
@@ -499,7 +483,7 @@ private static Object getValueOrDefault(boolean ownerIsKotlinType, Class<?> rawT
499483
return value == null && !ownerIsKotlinType && rawType.isPrimitive() ? ReflectionUtils.getPrimitiveDefault(rawType) : value;
500484
}
501485

502-
private AssociationHandler<Neo4jPersistentProperty> populateFrom(MapAccessor queryResult,
486+
private AssociationHandler<Neo4jPersistentProperty> populateFrom(MapAccessor queryResult, NodeDescription<?> baseDescription,
503487
PersistentPropertyAccessor<?> propertyAccessor, Predicate<Neo4jPersistentProperty> isConstructorParameter,
504488
boolean objectAlreadyMapped, Collection<Relationship> relationshipsFromResult, Collection<Node> nodesFromResult) {
505489

@@ -542,14 +526,14 @@ private AssociationHandler<Neo4jPersistentProperty> populateFrom(MapAccessor que
542526
return;
543527
}
544528

545-
createInstanceOfRelationships(persistentProperty, queryResult, (RelationshipDescription) association, relationshipsFromResult, nodesFromResult, null)
529+
createInstanceOfRelationships(persistentProperty, queryResult, (RelationshipDescription) association, baseDescription, relationshipsFromResult, nodesFromResult)
546530
.ifPresent(value -> propertyAccessor.setProperty(persistentProperty, value));
547531
};
548532
}
549533

550534
private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty persistentProperty, MapAccessor values,
551-
RelationshipDescription relationshipDescription, Collection<Relationship> relationshipsFromResult,
552-
Collection<Node> nodesFromResult, @Nullable NodeDescription<?> nodeDescription) {
535+
RelationshipDescription relationshipDescription, NodeDescription<?> baseDescription, Collection<Relationship> relationshipsFromResult,
536+
Collection<Node> nodesFromResult) {
553537

554538
String typeOfRelationship = relationshipDescription.getType();
555539
String sourceLabel = relationshipDescription.getSource().getPrimaryLabel();
@@ -583,13 +567,7 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
583567
mappedObjectHandler = (type, mappedObject) -> value.add(mappedObject);
584568
}
585569

586-
// Generate name driven by the (nullable) NodeDescription of necessary,
587-
// because it might contain associations not named CONCRETE_TYPE_TARGET but ABSTRACT_TYPE_TARGET
588-
String collectionName = relationshipDescription.generateRelatedNodesCollectionName(
589-
nodeDescription != null
590-
? nodeDescription
591-
: relationshipDescription.getSource()
592-
);
570+
String collectionName = relationshipDescription.generateRelatedNodesCollectionName(baseDescription);
593571

594572
Value list = values.get(collectionName);
595573

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ void shouldMatchPolymorphicKotlinInterfacesWhenFetchingAll(@Autowired CinemaRepo
406406
}
407407

408408
@Test // GH-2452
409-
void asdf(@Autowired ParentClassWithRelationshipRepository repository) {
409+
void loadAndPopulateRelationshipFromTheHierarchy(@Autowired ParentClassWithRelationshipRepository repository) {
410410

411411
long childId;
412412
try (Session session = driver.session(bookmarkCapture.createSessionConfig())) {

0 commit comments

Comments
 (0)