Skip to content

Commit 1af9a05

Browse files
GH-2578 - Document unwinding and pipelining of mapped __properties__.
Closes #2579.
1 parent 4351795 commit 1af9a05

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java

+41
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@
119119
import org.springframework.data.neo4j.integration.issues.gh2576.College;
120120
import org.springframework.data.neo4j.integration.issues.gh2576.CollegeRepository;
121121
import org.springframework.data.neo4j.integration.issues.gh2576.Student;
122+
import org.springframework.data.neo4j.integration.issues.gh2579.ColumnNode;
123+
import org.springframework.data.neo4j.integration.issues.gh2579.TableAndColumnRelation;
124+
import org.springframework.data.neo4j.integration.issues.gh2579.TableNode;
125+
import org.springframework.data.neo4j.integration.issues.gh2579.TableRepository;
122126
import org.springframework.data.neo4j.integration.misc.ConcreteImplementationTwo;
123127
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
124128
import org.springframework.data.neo4j.repository.query.QueryFragmentsAndParameters;
@@ -866,6 +870,43 @@ void listOfMapsShouldBeUsableAsArgumentsWithWorkaround(@Autowired Neo4jTemplate
866870
assertThat(uuids).containsExactly(student.getGuid());
867871
}
868872

873+
@Test
874+
@Tag("GH-2579")
875+
void unwindWithMergeShouldWork(@Autowired Neo4jTemplate template, @Autowired TableRepository tableRepository) {
876+
877+
TableNode tableNode = new TableNode();
878+
tableNode.setName("t1");
879+
tableNode.setSchemaName("a");
880+
tableNode.setSourceName("source1");
881+
tableNode = template.save(tableNode);
882+
883+
ColumnNode c1 = new ColumnNode();
884+
c1.setName("c1");
885+
c1.setSchemaName("a");
886+
c1.setSourceName("source1");
887+
c1.setTableName(tableNode.getName());
888+
long c1Id = template.save(c1).getId();
889+
890+
ColumnNode c2 = new ColumnNode();
891+
c2.setName("c2");
892+
c2.setSchemaName("a");
893+
c2.setSourceName("source2");
894+
c2.setTableName(tableNode.getName());
895+
long c2Id = template.save(c2).getId();
896+
897+
tableRepository.mergeTableAndColumnRelations(List.of(c1, c2), tableNode);
898+
899+
Optional<TableNode> resolvedTableNode = tableRepository.findById(tableNode.getId());
900+
assertThat(resolvedTableNode)
901+
.map(TableNode::getTableAndColumnRelation)
902+
.hasValueSatisfying(l -> {
903+
assertThat(l)
904+
.map(TableAndColumnRelation::getColumnNode)
905+
.map(ColumnNode::getId)
906+
.containsExactlyInAnyOrder(c1Id, c2Id);
907+
});
908+
}
909+
869910
@Configuration
870911
@EnableTransactionManagement
871912
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2579;
17+
18+
import lombok.Data;
19+
20+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
21+
import org.springframework.data.neo4j.core.schema.Id;
22+
import org.springframework.data.neo4j.core.schema.Node;
23+
24+
/**
25+
* @author Michael J. Simons
26+
*/
27+
@Data
28+
@Node("Column")
29+
public class ColumnNode {
30+
31+
@Id
32+
@GeneratedValue
33+
private Long id;
34+
35+
private String sourceName;
36+
37+
private String schemaName;
38+
39+
private String tableName;
40+
41+
private String name;
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2579;
17+
18+
import lombok.Data;
19+
20+
import org.springframework.data.neo4j.core.schema.RelationshipId;
21+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
22+
import org.springframework.data.neo4j.core.schema.TargetNode;
23+
24+
/**
25+
* @author Michael J. Simons
26+
*/
27+
@Data
28+
@RelationshipProperties
29+
public class TableAndColumnRelation {
30+
31+
@RelationshipId
32+
private Long id;
33+
34+
@TargetNode
35+
private ColumnNode columnNode;
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2579;
17+
18+
import lombok.Data;
19+
20+
import java.util.List;
21+
22+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
23+
import org.springframework.data.neo4j.core.schema.Id;
24+
import org.springframework.data.neo4j.core.schema.Node;
25+
import org.springframework.data.neo4j.core.schema.Relationship;
26+
27+
/**
28+
* @author Michael J. Simons
29+
*/
30+
@Data
31+
@Node("Table")
32+
public class TableNode {
33+
34+
@Id
35+
@GeneratedValue
36+
private Long id;
37+
38+
private String sourceName;
39+
40+
private String schemaName;
41+
42+
private String name;
43+
44+
private String tableComment;
45+
46+
@Relationship(type = "BELONG", direction = Relationship.Direction.INCOMING)
47+
private List<TableAndColumnRelation> tableAndColumnRelation;
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2011-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2579;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.neo4j.repository.Neo4jRepository;
21+
import org.springframework.data.neo4j.repository.query.Query;
22+
import org.springframework.data.repository.query.Param;
23+
24+
/**
25+
* @author Michael J. Simons
26+
*/
27+
public interface TableRepository extends Neo4jRepository<TableNode, Long> {
28+
29+
@Query(value = """
30+
UNWIND :#{#froms} AS col
31+
WITH col.__properties__ AS col, :#{#to}.__properties__ AS to
32+
MERGE (c:Column {
33+
sourceName: col.sourceName,
34+
schemaName: col.schemaName,
35+
tableName: col.tableName,
36+
name: col.name
37+
})
38+
MERGE (t:Table {
39+
sourceName: to.sourceName,
40+
schemaName: to.schemaName,
41+
name: to.name
42+
})
43+
MERGE (c) -[r:BELONG]-> (t)"""
44+
)
45+
void mergeTableAndColumnRelations(@Param("froms") List<ColumnNode> froms, @Param("to") TableNode to);
46+
}

0 commit comments

Comments
 (0)