Skip to content

Commit 4215593

Browse files
committed
spring-projectsGH-2436 - Add RelationshipId as shortcut for relationship properties ids.
Closes spring-projects#2436
1 parent 6b913fa commit 4215593

37 files changed

+121
-106
lines changed

src/main/asciidoc/appendix/custom-queries.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public final class Person {
233233
@RelationshipProperties
234234
public final class Actor {
235235
236-
@Id @GeneratedValue
236+
@RelationshipId
237237
private final Long id;
238238
239239
@TargetNode

src/main/asciidoc/faq/faq.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ public final class Person {
678678
@RelationshipProperties
679679
public final class Actor {
680680
681-
@Id @GeneratedValue
681+
@RelationshipId
682682
private final Long id;
683683
684684
@TargetNode

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,10 @@ A relationship property class and its usage may look like this:
223223
include::../../../../src/test/java/org/springframework/data/neo4j/documentation/domain/Roles.java[tags=mapping.relationship.properties]
224224
----
225225

226-
You must define a property for the generated, internal ID so that SDN can determine during save which relationships
226+
You must define a property for the generated, internal ID (`@RelationshipId`) so that SDN can determine during save which relationships
227227
can be safely overwritten without losing properties.
228228
If SDN does not find a field for storing the internal node id, it will fail during startup.
229229

230-
NOTE: The only supported generated ID field on classes annotated with `@RelationshipProperties` is `@GeneratedValue` with
231-
using the default ID generator `InternalIdGenerator` as shown above. Other generators will lead to a failure during
232-
startup.
233-
234230
.Defining relationship properties for an entity
235231
[source,java,indent=0]
236232
----

src/main/java/org/springframework/data/neo4j/core/schema/GeneratedValue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @since 6.0
4040
*/
4141
@Retention(RetentionPolicy.RUNTIME)
42-
@Target(ElementType.FIELD)
42+
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
4343
@Documented
4444
@Inherited
4545
@API(status = API.Status.STABLE, since = "6.0")

src/main/java/org/springframework/data/neo4j/core/schema/Id.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
* @since 6.0
7373
*/
7474
@Retention(RetentionPolicy.RUNTIME)
75-
@Target(ElementType.FIELD)
75+
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
7676
@Documented
7777
@Inherited
7878
@org.springframework.data.annotation.Id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.schema;
17+
18+
import org.apiguardian.api.API;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Inherited;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* A combined annotation for id fields in {@link RelationshipProperties} classes.
29+
*
30+
* @author Gerrit Meier
31+
* @since 6.2
32+
*/
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target(ElementType.FIELD)
35+
@Documented
36+
@Inherited
37+
@Id
38+
@GeneratedValue
39+
@API(status = API.Status.STABLE, since = "6.2")
40+
public @interface RelationshipId {
41+
}

src/test/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentEntityTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.neo4j.core.schema.Node;
4141
import org.springframework.data.neo4j.core.schema.Property;
4242
import org.springframework.data.neo4j.core.schema.Relationship;
43+
import org.springframework.data.neo4j.core.schema.RelationshipId;
4344
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
4445
import org.springframework.data.neo4j.core.schema.TargetNode;
4546

@@ -576,7 +577,7 @@ static class EntityWithInCorrectRelationshipProperties {
576577
@RelationshipProperties
577578
static class HasTargetNodeRelationshipProperties {
578579

579-
@Id @GeneratedValue
580+
@RelationshipId
580581
private Long id;
581582

582583
@TargetNode
@@ -586,7 +587,7 @@ static class HasTargetNodeRelationshipProperties {
586587
@RelationshipProperties
587588
static class HasNoTargetNodeRelationshipProperties {
588589

589-
@Id @GeneratedValue
590+
@RelationshipId
590591
private Long id;
591592
}
592593

@@ -650,7 +651,7 @@ static class OtherEntityWithBidirectionalRelationshipWithRelationshipProperties
650651

651652
@RelationshipProperties
652653
static class OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties {
653-
@Id @GeneratedValue
654+
@RelationshipId
654655
private Long id;
655656

656657
@TargetNode
@@ -659,7 +660,7 @@ static class OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesP
659660

660661
@RelationshipProperties
661662
static class EntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties {
662-
@Id @GeneratedValue
663+
@RelationshipId
663664
private Long id;
664665

665666
@TargetNode
@@ -683,7 +684,7 @@ static class EntityWithBidirectionalRelationshipProperties {
683684
@RelationshipProperties
684685
static class BidirectionalRelationshipProperties {
685686

686-
@Id @GeneratedValue
687+
@RelationshipId
687688
private Long id;
688689

689690
@TargetNode

src/test/java/org/springframework/data/neo4j/core/mapping/Neo4jMappingContextTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.data.neo4j.core.schema.Node;
6262
import org.springframework.data.neo4j.core.schema.Property;
6363
import org.springframework.data.neo4j.core.schema.Relationship;
64+
import org.springframework.data.neo4j.core.schema.RelationshipId;
6465
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
6566
import org.springframework.data.neo4j.core.schema.TargetNode;
6667
import org.springframework.data.neo4j.integration.shared.common.FriendshipRelationship;
@@ -808,7 +809,7 @@ static class IrrelevantSourceContainer2 {
808809

809810
@RelationshipProperties
810811
static class RelationshipPropertyContainer {
811-
@Id @GeneratedValue @SuppressWarnings("unused")
812+
@RelationshipId @SuppressWarnings("unused")
812813
private Long id;
813814

814815
@TargetNode
@@ -896,8 +897,8 @@ public static class ConcreteEntity extends SomeBaseEntity {
896897
}
897898

898899
public static class RelationshipPropertiesBaseClass<T extends SomeBaseEntity> {
899-
@Id
900-
@GeneratedValue
900+
901+
@RelationshipId
901902
public Long internalId;
902903

903904
public String id;

src/test/java/org/springframework/data/neo4j/core/mapping/datagraph1446/AbstractR.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.neo4j.core.mapping.datagraph1446;
1717

18-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19-
import org.springframework.data.neo4j.core.schema.Id;
18+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2019
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2120
import org.springframework.data.neo4j.core.schema.TargetNode;
2221

@@ -27,7 +26,7 @@
2726
@RelationshipProperties
2827
public abstract class AbstractR<T> {
2928

30-
@Id @GeneratedValue
29+
@RelationshipId
3130
private Long id;
3231

3332
@TargetNode

src/test/java/org/springframework/data/neo4j/core/mapping/datagraph1448/R_S3.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.neo4j.core.mapping.datagraph1448;
1717

18-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19-
import org.springframework.data.neo4j.core.schema.Id;
18+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2019
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2120
import org.springframework.data.neo4j.core.schema.TargetNode;
2221

@@ -27,7 +26,7 @@
2726
@RelationshipProperties
2827
public class R_S3<T extends RelatedThing> {
2928

30-
@Id @GeneratedValue
29+
@RelationshipId
3130
private Long id;
3231

3332
@TargetNode

src/test/java/org/springframework/data/neo4j/documentation/domain/Roles.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
21-
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2221
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2322
import org.springframework.data.neo4j.core.schema.TargetNode;
2423

@@ -29,7 +28,7 @@
2928
@RelationshipProperties
3029
public class Roles {
3130

32-
@Id @GeneratedValue
31+
@RelationshipId
3332
private Long id;
3433

3534
private final List<String> roles;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.data.neo4j.core.schema.GeneratedValue;
3030
import org.springframework.data.neo4j.core.schema.Id;
3131
import org.springframework.data.neo4j.core.schema.Node;
32+
import org.springframework.data.neo4j.core.schema.RelationshipId;
3233
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
3334
import org.springframework.data.neo4j.core.schema.TargetNode;
3435
import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager;
@@ -149,8 +150,7 @@ static class CollectionChildNodeA {
149150
@RelationshipProperties
150151
static class RelProperties {
151152

152-
@Id
153-
@GeneratedValue
153+
@RelationshipId
154154
Long id;
155155

156156
@TargetNode

src/test/java/org/springframework/data/neo4j/integration/issues/gh2210/GH2210IT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import org.springframework.data.neo4j.core.Neo4jTemplate;
2929
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
3030
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
31-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
3231
import org.springframework.data.neo4j.core.schema.Id;
3332
import org.springframework.data.neo4j.core.schema.Node;
3433
import org.springframework.data.neo4j.core.schema.Relationship;
34+
import org.springframework.data.neo4j.core.schema.RelationshipId;
3535
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
3636
import org.springframework.data.neo4j.core.schema.TargetNode;
3737
import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager;
@@ -217,7 +217,7 @@ public Set<SomeRelation> getSomeRelationsOut() {
217217
@RelationshipProperties
218218
static class SomeRelation {
219219

220-
@Id @GeneratedValue
220+
@RelationshipId
221221
private Long id;
222222

223223
private String someData;

src/test/java/org/springframework/data/neo4j/integration/issues/gh2289/RangeRelation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
import lombok.Data;
1919

20-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
21-
import org.springframework.data.neo4j.core.schema.Id;
2220
import org.springframework.data.neo4j.core.schema.Property;
21+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2322
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2423
import org.springframework.data.neo4j.core.schema.TargetNode;
2524

@@ -29,7 +28,8 @@
2928
@Data // lombok
3029
@RelationshipProperties
3130
public class RangeRelation {
32-
@Id @GeneratedValue private Long id;
31+
@RelationshipId
32+
private Long id;
3333

3434
@Property private double minDelta;
3535
@Property private double maxDelta;

src/test/java/org/springframework/data/neo4j/integration/issues/gh2289/RangeRelationRO.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import lombok.Data;
1919
import lombok.EqualsAndHashCode;
2020

21-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
22-
import org.springframework.data.neo4j.core.schema.Id;
2321
import org.springframework.data.neo4j.core.schema.Property;
22+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2423
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2524
import org.springframework.data.neo4j.core.schema.TargetNode;
2625

@@ -32,7 +31,8 @@
3231
public class RangeRelationRO {
3332

3433
@EqualsAndHashCode.Exclude
35-
@Id @GeneratedValue private Long id;
34+
@RelationshipId
35+
private Long id;
3636

3737
@Property private double minDelta;
3838
@Property private double maxDelta;

src/test/java/org/springframework/data/neo4j/integration/issues/gh2323/Knows.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.issues.gh2323;
1717

18-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19-
import org.springframework.data.neo4j.core.schema.Id;
18+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2019
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2120
import org.springframework.data.neo4j.core.schema.TargetNode;
2221

@@ -26,7 +25,7 @@
2625
@RelationshipProperties
2726
public class Knows {
2827

29-
@Id @GeneratedValue
28+
@RelationshipId
3029
private Long id;
3130

3231
private final String description;

src/test/java/org/springframework/data/neo4j/integration/movies/shared/Actor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
22-
import org.springframework.data.neo4j.core.schema.Id;
21+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2322
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2423
import org.springframework.data.neo4j.core.schema.TargetNode;
2524

@@ -30,7 +29,7 @@
3029
@RelationshipProperties
3130
public final class Actor {
3231

33-
@Id @GeneratedValue
32+
@RelationshipId
3433
private Long id;
3534

3635
@TargetNode

src/test/java/org/springframework/data/neo4j/integration/properties/DomainClasses.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.data.neo4j.core.schema.Node;
3232
import org.springframework.data.neo4j.core.schema.Property;
3333
import org.springframework.data.neo4j.core.schema.Relationship;
34+
import org.springframework.data.neo4j.core.schema.RelationshipId;
3435
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
3536
import org.springframework.data.neo4j.core.schema.TargetNode;
3637
import org.springframework.data.neo4j.core.support.DateLong;
@@ -95,7 +96,7 @@ static class IrrelevantTargetContainer {
9596
@Getter @Setter
9697
static class RelationshipPropertyContainer extends BaseClass {
9798

98-
private @Id @GeneratedValue Long id;
99+
private @RelationshipId Long id;
99100

100101
@TargetNode
101102
private IrrelevantTargetContainer irrelevantTargetContainer;

src/test/java/org/springframework/data/neo4j/integration/shared/common/AltLikedByPersonRelationship.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.shared.common;
1717

18-
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19-
import org.springframework.data.neo4j.core.schema.Id;
18+
import org.springframework.data.neo4j.core.schema.RelationshipId;
2019
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2120
import org.springframework.data.neo4j.core.schema.TargetNode;
2221

2322
import java.util.Objects;
2423

2524
/**
26-
* @@author Michael J. Simons
25+
* @author Michael J. Simons
2726
*/
2827
@RelationshipProperties
2928
public class AltLikedByPersonRelationship {
3029

31-
@Id @GeneratedValue
30+
@RelationshipId
3231
private Long id;
3332

3433
private Integer rating;

0 commit comments

Comments
 (0)