Skip to content

Commit 4215593

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

37 files changed

+121
-106
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
Lines changed: 41 additions & 0 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
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

0 commit comments

Comments
 (0)