Skip to content

Commit

Permalink
fix: Double check for a collection to be present in the mapping witho…
Browse files Browse the repository at this point in the history
…ut directional prefix.

This is a direct follow up on #2918, in which we added the suffix to the collection names. That change didn’t take custom queries into account and people are most likely still using the known pattern of `source_REL_target` and I would like to not break them.
Therefor, we check now in the values list if a collection without suffix exists if we didn’t find one with and if so, use that.

This fixes #2963.
  • Loading branch information
michael-simons committed Oct 24, 2024
1 parent 8d74680 commit 9b23c03
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ private Optional<Object> createInstanceOfRelationships(Neo4jPersistentProperty p
String collectionName = relationshipDescription.generateRelatedNodesCollectionName(baseDescription);
Value list = values.get(collectionName);
boolean relationshipListEmptyOrNull = Values.NULL.equals(list);
if (relationshipListEmptyOrNull) {
collectionName = collectionName.replaceFirst("_" + relationshipDescription.isOutgoing() + "\\z", "");
}
list = values.get(collectionName);
relationshipListEmptyOrNull = Values.NULL.equals(list);

List<Object> relationshipsAndProperties = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@
import org.springframework.data.neo4j.integration.issues.gh2908.Place;
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.gh2963.MyModel;
import org.springframework.data.neo4j.integration.issues.gh2963.MyRepository;
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 @@ -1336,6 +1338,25 @@ void shouldNotGenerateDuplicateOrder(@Autowired LocatedNodeRepository repository
}
}

@Tag("GH-2963")
@Test
void customQueriesShouldKeepWorkingWithoutSpecifyingTheRelDirectionInTheirQueries(@Autowired MyRepository myRepository) {
// set up data in database
MyModel myNestedModel = new MyModel();
myNestedModel.setName("nested");

MyModel myRootModel = new MyModel();
myRootModel.setName("root");
myRootModel.setMyNestedModel(myNestedModel);

String uuid = myRepository.save(myRootModel).getUuid();
Optional<MyModel> rootModelFromDbCustom = myRepository.getByUuidCustomQuery(uuid);
assertThat(rootModelFromDbCustom).map(MyModel::getMyNestedModel).isPresent();

rootModelFromDbCustom = myRepository.getByUuidCustomQueryV2(uuid);
assertThat(rootModelFromDbCustom).map(MyModel::getMyNestedModel).isPresent();
}

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.gh2963;

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 Andreas Rümpel
* @author Michael J. Simons
*/
@Node
public class MyModel {
@Id
@GeneratedValue(generatorClass = UUIDStringGenerator.class)
private String uuid;

private String name;

@Relationship(value = "REL_TO_MY_NESTED_MODEL")
private MyModel myNestedModel;

public MyModel getMyNestedModel() {
return myNestedModel;
}

public void setMyNestedModel(MyModel myNestedModel) {
this.myNestedModel = myNestedModel;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.gh2963;

import java.util.Optional;

import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

/**
* @author Andreas Rümpel
* @author Michael J. Simons
*/
public interface MyRepository extends CrudRepository<MyModel, String> {

@Query("""
MATCH (root:MyModel {uuid: $uuid})
RETURN root {
.*, MyModel_REL_TO_MY_NESTED_MODEL_MyModel: [
(root)-[:REL_TO_MY_NESTED_MODEL]->(nested:MyModel) | nested {. *}
]
}
""")
Optional<MyModel> getByUuidCustomQuery(String uuid);

@Query("""
MATCH (root:MyModel {uuid: $uuid})
RETURN root {
.*, MyModel_REL_TO_MY_NESTED_MODEL_MyModel_true: [
(root)-[:REL_TO_MY_NESTED_MODEL]->(nested:MyModel) | nested {. *}
]
}
""")
Optional<MyModel> getByUuidCustomQueryV2(String uuid);
}

0 comments on commit 9b23c03

Please sign in to comment.