Skip to content

Commit 408f2d0

Browse files
committed
GH-2313 - Enable multi-level projections.
Closes #2313
1 parent 6d1b23e commit 408f2d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1805
-265
lines changed

src/main/asciidoc/object-mapping/projections.adoc

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,66 @@ can add additional properties.
2929
The rules are as follows: first, the properties of the domain type are used to populate the DTO. In case the DTO declares
3030
additional properties - via accessors or fields - Spring Data Neo4j looks in the resulting record for matching properties.
3131
Properties must match exactly by name and can be of simple types (as defined in `org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes`)
32-
or of known persistent entites. Collections of those are supported, but maps are not.
32+
or of known persistent entities. Collections of those are supported, but maps are not.
33+
34+
[[projections.sdn.multi-level]]
35+
== Multi-level projections
36+
37+
Spring Data Neo4j also supports multi-level projections.
38+
39+
[source,java]
40+
.Example of multi-level projection
41+
----
42+
interface ProjectionWithNestedProjection {
43+
44+
String getName();
45+
46+
List<Subprojection1> getLevel1();
47+
48+
interface Subprojection1 {
49+
String getName();
50+
List<Subprojection2> getLevel2();
51+
}
52+
53+
interface Subprojection2 {
54+
String getName();
55+
}
56+
}
57+
----
58+
59+
Even though it is possible to model cyclic projections or point towards entities that will create a cycle,
60+
the projection logic will not follow those cycles but only create cycle-free queries.
61+
62+
Multi-level projections are bounded to the entities they should project.
63+
`RelationshipProperties` fall into the category of entities in this case and needs to get respected if projections get applied.
64+
65+
[[projections.sdn.persistence]]
66+
== Persistence of projections
67+
68+
Analogue to the retrieval of data via projections, they can also be used as a blueprint for persistence.
69+
The `Neo4jTemplate` offers a fluent API to apply those projections to a save operation.
70+
71+
You could either save a projection for a given domain class
72+
73+
[source,java]
74+
.Save projection for a given domain class
75+
----
76+
Projection projection = neo4jTemplate.save(DomainClass.class).one(projectionValue);
77+
----
78+
79+
or you could save a domain object but only respect the fields defined in the projection.
80+
81+
[source,java]
82+
.Save domain object with a given projection blueprint
83+
----
84+
Projection projection = neo4jTemplate.saveAs(domainObject, Projection.class);
85+
----
86+
87+
In both cases, that also are available for collection based operations, only the fields and relationships
88+
defined in the projection will get updated.
89+
90+
NOTE: To prevent deletion of data (e.g. removal of relationships),
91+
you should always load at least all the data that should get persisted later.
3392

3493
[[projections.sdn.full-example]]
3594
== A full example

src/main/java/org/springframework/data/neo4j/core/FluentFindOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
/**
2828
* {@link FluentFindOperation} allows creation and execution of Neo4j find operations in a fluent API style.
29-
* <br />
29+
* <p>
3030
* The starting {@literal domainType} is used for mapping the query provided via {@code by} into the
3131
* Neo4j specific representation. By default, the originating {@literal domainType} is also used for mapping back the
3232
* result. However, it is possible to define an different {@literal returnType} via
33-
* {@code as} to mapping the result.<br />
33+
* {@code as} to mapping the result.
3434
*
3535
* @author Michael Simons
3636
* @since 6.1

src/main/java/org/springframework/data/neo4j/core/FluentFindOperationSupport.java renamed to src/main/java/org/springframework/data/neo4j/core/FluentOperationSupport.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
* @author Michael J. Simons
2828
* @since 6.1
2929
*/
30-
final class FluentFindOperationSupport implements FluentFindOperation {
30+
final class FluentOperationSupport implements FluentFindOperation, FluentSaveOperation {
3131

3232
private final Neo4jTemplate template;
3333

34-
FluentFindOperationSupport(Neo4jTemplate template) {
34+
FluentOperationSupport(Neo4jTemplate template) {
3535
this.template = template;
3636
}
3737

@@ -98,4 +98,43 @@ private List<T> doFind(TemplateSupport.FetchType fetchType) {
9898
return template.doFind(query, parameters, domainType, returnType, fetchType);
9999
}
100100
}
101+
102+
@Override
103+
public <T> ExecutableSave<T> save(Class<T> domainType) {
104+
105+
Assert.notNull(domainType, "DomainType must not be null!");
106+
107+
return new ExecutableSaveSupport<>(this.template, domainType);
108+
}
109+
110+
private static class ExecutableSaveSupport<DT> implements ExecutableSave<DT> {
111+
112+
private final Neo4jTemplate template;
113+
private final Class<DT> domainType;
114+
115+
ExecutableSaveSupport(Neo4jTemplate template, Class<DT> domainType) {
116+
this.template = template;
117+
this.domainType = domainType;
118+
}
119+
120+
@Override
121+
public <T> T one(T instance) {
122+
123+
List<T> result = doSave(Collections.singleton(instance));
124+
if (result.isEmpty()) {
125+
return null;
126+
}
127+
return result.get(0);
128+
}
129+
130+
@Override
131+
public <T> List<T> all(Iterable<T> instances) {
132+
133+
return doSave(instances);
134+
}
135+
136+
private <T> List<T> doSave(Iterable<T> instances) {
137+
return template.doSave(instances, domainType);
138+
}
139+
}
101140
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2011-2021 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.core;
17+
18+
import java.util.List;
19+
20+
import org.apiguardian.api.API;
21+
22+
/**
23+
* {@link FluentSaveOperation} allows creation and execution of Neo4j save operations in a fluent API style. It
24+
* is designed to be used together with the {@link FluentFindOperation fluent find operations}.
25+
* <p>
26+
* Both interfaces provide a way to specify a pair of two types: A domain type and a result (projected) type.
27+
* The fluent save operations are mainly used with DTO based projections. Closed interface projections won't be that
28+
* helpful when you received them via {@link FluentFindOperation fluent find operations} as they won't be modifiable.
29+
*
30+
* @author Michael J. Simons
31+
* @since TBA
32+
*/
33+
@API(status = API.Status.STABLE, since = "TBA")
34+
public interface FluentSaveOperation {
35+
36+
/**
37+
* Start creating a save operation for the given {@literal domainType}.
38+
*
39+
* @param domainType must not be {@literal null}.
40+
* @return new instance of {@link ExecutableSave}.
41+
* @throws IllegalArgumentException if domainType is {@literal null}.
42+
*/
43+
<T> ExecutableSave<T> save(Class<T> domainType);
44+
45+
/**
46+
* After the domain type has been specified, related projections or instances of the domain type can be saved.
47+
*
48+
* @param <DT> the domain type
49+
*/
50+
interface ExecutableSave<DT> {
51+
52+
/**
53+
* @param instance The instance to be saved
54+
* @param <T> The type of the instance passed to this method. It should be the same as the domain type before
55+
* or a projection of the domain type. If they are not related, the results may be undefined.
56+
* @return The saved instance, can also be a new object, so you are recommended to use this instance after
57+
* the save operation
58+
*/
59+
<T> T one(T instance);
60+
61+
/**
62+
* @param instances The instances to be saved
63+
* @param <T> The type of the instances passed to this method. It should be the same as the domain type before
64+
* or a projection of the domain type. If they are not related, the results may be undefined.
65+
* @return The saved instances, can also be a new objects, so you are recommended to use those instances
66+
* after the save operation
67+
*/
68+
<T> List<T> all(Iterable<T> instances);
69+
}
70+
}

0 commit comments

Comments
 (0)