Skip to content

Commit

Permalink
fix: Use direction as part of the node collection name for each relat…
Browse files Browse the repository at this point in the history
…ionship.

This avoids having duplicate keys in the map projection used to collect the
nodes for each relationship in cases where a node has two relationships with the
same name to the same label in different directions.

Fixes #2918.

Co-authored-by: Mathias Kühn <[email protected]>
  • Loading branch information
michael-simons and ma-ku committed Jul 10, 2024
1 parent 2fe421f commit d393f55
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ default boolean isIncoming() {
@NonNull
default String generateRelatedNodesCollectionName(NodeDescription<?> mostAbstractNodeDescription) {

return this.getSource().getMostAbstractParentLabel(mostAbstractNodeDescription) + "_" + this.getType() + "_" + this.getTarget().getPrimaryLabel();
return this.getSource().getMostAbstractParentLabel(mostAbstractNodeDescription) + "_" + this.getType() + "_" + this.getTarget().getPrimaryLabel() + "_" + this.isOutgoing();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.tuple;

import java.util.ArrayList;
Expand Down Expand Up @@ -151,6 +152,8 @@
import org.springframework.data.neo4j.integration.issues.gh2886.Apple;
import org.springframework.data.neo4j.integration.issues.gh2886.FruitRepository;
import org.springframework.data.neo4j.integration.issues.gh2886.Orange;
import org.springframework.data.neo4j.integration.issues.gh2918.ConditionNode;
import org.springframework.data.neo4j.integration.issues.gh2918.ConditionRepository;
import org.springframework.data.neo4j.integration.issues.qbe.A;
import org.springframework.data.neo4j.integration.issues.qbe.ARepository;
import org.springframework.data.neo4j.integration.issues.qbe.B;
Expand Down Expand Up @@ -1217,6 +1220,18 @@ void dynamicLabels(@Autowired FruitRepository repository) {
assertThat(fruits).allMatch(f -> f instanceof Apple || f instanceof Orange);
}

@Test
@Tag("GH-2918")
void loadCycleFreeWithInAndOutgoingRelationship(@Autowired ConditionRepository conditionRepository) {

var conditionSaved = conditionRepository.save(new ConditionNode());

// Condition has both an incoming and outgoing relationship typed CAUSES that will cause a duplicate key
// in the map projection for the relationships to load. The fix was to indicate the direction in the name
// used for projecting the relationship, too
assertThatNoException().isThrownBy(() -> conditionRepository.findById(conditionSaved.uuid));
}

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2918;

import java.util.Set;

import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;

/**
* @author Mathias Kühn
*/
@Node
public class ConditionNode {
@Id
@GeneratedValue(UUIDStringGenerator.class)
public String uuid;

@Relationship(type = "CAUSES", direction = Relationship.Direction.INCOMING)
public Set<FailureRelationship> upstreamFailures;

@Relationship(type = "CAUSES", direction = Relationship.Direction.OUTGOING)
public Set<FailureRelationship> downstreamFailures;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2918;

import org.springframework.data.neo4j.repository.Neo4jRepository;

/**
* @author Mathias Kühn
*/
public interface ConditionRepository extends Neo4jRepository<ConditionNode, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2918;

import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.support.UUIDStringGenerator;

/**
* @author Mathias Kühn
*/
@Node
public class FailureNode {
@Id
@GeneratedValue(UUIDStringGenerator.class)
public String uuid;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.integration.issues.gh2918;

import org.springframework.data.neo4j.core.schema.RelationshipId;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;

/**
* @author Mathias Kühn
*/
@RelationshipProperties
public abstract class FailureRelationship {

@RelationshipId
public String id;

@TargetNode
public FailureNode target;
}

0 comments on commit d393f55

Please sign in to comment.