Skip to content

Commit 0a95db2

Browse files
committed
DATAGRAPH-1425 - Add strict entity support.
1 parent fa6a1aa commit 0a95db2

File tree

128 files changed

+871
-787
lines changed

Some content is hidden

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

128 files changed

+871
-787
lines changed

src/main/java/org/springframework/data/neo4j/config/Neo4jConfigurationSupport.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
2727
import org.springframework.core.type.filter.AnnotationTypeFilter;
28+
import org.springframework.data.annotation.Persistent;
2829
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
2930
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
3031
import org.springframework.data.neo4j.core.schema.Node;
32+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
3133
import org.springframework.util.ClassUtils;
3234
import org.springframework.util.StringUtils;
3335

@@ -115,6 +117,8 @@ protected final Set<Class<?>> scanForEntities(String basePackage) throws ClassNo
115117
ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
116118
false);
117119
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Node.class));
120+
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
121+
componentProvider.addIncludeFilter(new AnnotationTypeFilter(RelationshipProperties.class));
118122

119123
ClassLoader classLoader = Neo4jConfigurationSupport.class.getClassLoader();
120124
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.stream.Collectors;
3030
import java.util.stream.Stream;
3131

32+
import org.springframework.data.annotation.Persistent;
3233
import org.springframework.data.mapping.Association;
3334
import org.springframework.data.mapping.PropertyHandler;
3435
import org.springframework.data.mapping.model.BasicPersistentEntity;
@@ -89,7 +90,7 @@ final class DefaultNeo4jPersistentEntity<T> extends BasicPersistentEntity<T, Neo
8990
DefaultNeo4jPersistentEntity(TypeInformation<T> information) {
9091
super(information);
9192

92-
this.isExplicitEntity = this.isAnnotationPresent(Node.class);
93+
this.isExplicitEntity = this.isAnnotationPresent(Node.class) || this.isAnnotationPresent(Persistent.class);
9394
this.primaryLabel = computePrimaryLabel();
9495
this.additionalLabels = Lazy.of(this::computeAdditionalLabels);
9596
this.graphProperties = Lazy.of(this::computeGraphProperties);

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jPersistentProperty.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.mapping.Association;
2424
import org.springframework.data.mapping.MappingException;
2525
import org.springframework.data.mapping.PersistentEntity;
26+
import org.springframework.data.mapping.PersistentProperty;
2627
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
2728
import org.springframework.data.mapping.model.Property;
2829
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -95,31 +96,30 @@ protected Association<Neo4jPersistentProperty> createAssociation() {
9596
Class<?> type = getRelationshipPropertiesTargetType(getActualType());
9697
obverseOwner = this.mappingContext.getPersistentEntity(type);
9798
relationshipPropertiesClass = this.mappingContext.getPersistentEntity(getActualType());
98-
} else if (dynamicAssociation) {
99+
} else {
100+
Class<?> associationTargetType = this.getAssociationTargetType();
101+
obverseOwner = this.mappingContext.addPersistentEntity(associationTargetType).get();
102+
if (dynamicAssociation) {
99103

100-
TypeInformation<?> mapValueType = this.getTypeInformation().getMapValueType();
104+
TypeInformation<?> mapValueType = this.getTypeInformation().getMapValueType();
101105

102-
boolean relationshipPropertiesCollection =
103-
this.mappingContext.getPersistentEntity(mapValueType.getActualType().getType())
104-
.isRelationshipPropertiesEntity();
106+
boolean relationshipPropertiesCollection =
107+
this.mappingContext.getPersistentEntity(mapValueType.getActualType().getType())
108+
.isRelationshipPropertiesEntity();
105109

106-
boolean relationshipPropertiesScalar =
107-
mapValueType.getType().isAnnotationPresent(RelationshipProperties.class);
110+
boolean relationshipPropertiesScalar =
111+
mapValueType.getType().isAnnotationPresent(RelationshipProperties.class);
108112

109-
if (relationshipPropertiesCollection) {
110-
Class<?> type = getRelationshipPropertiesTargetType(mapValueType.getActualType().getType());
111-
obverseOwner = this.mappingContext.getPersistentEntity(type);
112-
relationshipPropertiesClass = this.mappingContext
113-
.getPersistentEntity(mapValueType.getComponentType().getType());
113+
if (relationshipPropertiesCollection) {
114+
Class<?> type = getRelationshipPropertiesTargetType(mapValueType.getActualType().getType());
115+
obverseOwner = this.mappingContext.getPersistentEntity(type);
116+
relationshipPropertiesClass = this.mappingContext
117+
.getPersistentEntity(mapValueType.getComponentType().getType());
114118

115-
} else if (relationshipPropertiesScalar) {
116-
obverseOwner = this.mappingContext.getPersistentEntity(this.getAssociationTargetType());
117-
relationshipPropertiesClass = this.mappingContext.getPersistentEntity(mapValueType.getType());
118-
} else {
119-
obverseOwner = this.mappingContext.getPersistentEntity(this.getAssociationTargetType());
119+
} else if (relationshipPropertiesScalar) {
120+
relationshipPropertiesClass = this.mappingContext.getPersistentEntity(mapValueType.getType());
121+
}
120122
}
121-
} else {
122-
obverseOwner = this.mappingContext.getPersistentEntity(this.getAssociationTargetType());
123123
}
124124

125125
Relationship outgoingRelationship = this.findAnnotation(Relationship.class);
@@ -154,12 +154,11 @@ protected Association<Neo4jPersistentProperty> createAssociation() {
154154

155155
@NonNull
156156
private Class<?> getRelationshipPropertiesTargetType(Class<?> relationshipPropertiesType) {
157-
Neo4jPersistentProperty persistentProperty = this.mappingContext.getPersistentEntity(relationshipPropertiesType)
158-
.getPersistentProperty(TargetNode.class);
159-
if (persistentProperty == null) {
160-
throw new MappingException("Missing @TargetNode declaration in " + relationshipPropertiesType);
161-
}
162-
return persistentProperty.getType();
157+
return this.mappingContext.addPersistentEntity(relationshipPropertiesType)
158+
.map(entity -> entity.getPersistentProperty(TargetNode.class))
159+
.map(PersistentProperty::getType)
160+
.orElseThrow(
161+
() -> new MappingException("Missing @TargetNode declaration in " + relationshipPropertiesType));
163162
}
164163

165164
@Override

src/main/java/org/springframework/data/neo4j/core/mapping/Neo4jMappingContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ protected <T> Neo4jPersistentEntity<?> createPersistentEntity(TypeInformation<T>
179179
Class<? super T> superclass = typeInformation.getType().getSuperclass();
180180

181181
if (isValidParentNode(superclass)) {
182-
Neo4jPersistentEntity<?> parentNodeDescription = getPersistentEntity(superclass);
183-
if (parentNodeDescription != null) {
182+
addPersistentEntity(superclass).ifPresent(parentNodeDescription -> {
184183
parentNodeDescription.addChildNodeDescription(newEntity);
185184
newEntity.setParentNodeDescription(parentNodeDescription);
186-
}
185+
});
186+
187187
}
188188

189189
return newEntity;

src/main/java/org/springframework/data/neo4j/repository/config/Neo4jRepositoryConfigurationExtension.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package org.springframework.data.neo4j.repository.config;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.Arrays;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122

2223
import org.apiguardian.api.API;
2324
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2425
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26+
import org.springframework.data.annotation.Persistent;
2527
import org.springframework.data.neo4j.core.schema.Node;
28+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2629
import org.springframework.data.neo4j.repository.Neo4jRepository;
2730
import org.springframework.data.neo4j.repository.support.Neo4jRepositoryFactoryBean;
2831
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
@@ -83,7 +86,7 @@ protected String getModulePrefix() {
8386

8487
@Override
8588
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
86-
return Collections.singleton(Node.class);
89+
return Arrays.asList(Node.class, RelationshipProperties.class, Persistent.class);
8790
}
8891

8992
@Override

src/main/java/org/springframework/data/neo4j/repository/config/ReactiveNeo4jRepositoryConfigurationExtension.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package org.springframework.data.neo4j.repository.config;
1717

1818
import java.lang.annotation.Annotation;
19+
import java.util.Arrays;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122

2223
import org.apiguardian.api.API;
2324
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2425
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
26+
import org.springframework.data.annotation.Persistent;
2527
import org.springframework.data.neo4j.core.schema.Node;
28+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
2629
import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;
2730
import org.springframework.data.neo4j.repository.support.ReactiveNeo4jRepositoryFactoryBean;
2831
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
@@ -83,7 +86,7 @@ protected String getModulePrefix() {
8386

8487
@Override
8588
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
86-
return Collections.singleton(Node.class);
89+
return Arrays.asList(Node.class, RelationshipProperties.class, Persistent.class);
8790
}
8891

8992
@Override

src/main/java/org/springframework/data/neo4j/repository/query/DtoInstantiatingConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public Object convert(EntityInstanceWithSource entityInstanceAndSource) {
7474

7575
PersistentEntity<?, ?> sourceEntity = context.getRequiredPersistentEntity(entityInstance.getClass());
7676
PersistentPropertyAccessor sourceAccessor = sourceEntity.getPropertyAccessor(entityInstance);
77-
PersistentEntity<?, ?> targetEntity = context.getRequiredPersistentEntity(targetType);
77+
78+
PersistentEntity<?, ?> targetEntity = context.addPersistentEntity(targetType).get();
7879
PreferredConstructor<?, ? extends PersistentProperty<?>> constructor = targetEntity
7980
.getPersistenceConstructor();
8081

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ private static class Child extends BaseClass {
386386
private String name;
387387
}
388388

389+
@Node
389390
static class TypeWithInvalidDynamicRelationshipMappings1 {
390391

391392
@Id private String id;
@@ -395,6 +396,7 @@ static class TypeWithInvalidDynamicRelationshipMappings1 {
395396
private Map<String, Neo4jMappingContextTest.BikeNode> bikes2;
396397
}
397398

399+
@Node
398400
static class TypeWithInvalidDynamicRelationshipMappings2 {
399401

400402
@Id private String id;
@@ -404,6 +406,7 @@ static class TypeWithInvalidDynamicRelationshipMappings2 {
404406
private Map<String, List<Neo4jMappingContextTest.BikeNode>> bikes2;
405407
}
406408

409+
@Node
407410
static class TypeWithInvalidDynamicRelationshipMappings3 {
408411

409412
@Id private String id;
@@ -413,11 +416,13 @@ static class TypeWithInvalidDynamicRelationshipMappings3 {
413416
private Map<String, List<Neo4jMappingContextTest.BikeNode>> bikes2;
414417
}
415418

419+
@Node
416420
static class EntityWithCorrectRelationshipProperties {
417421
@Id private String id;
418422
@Relationship HasTargetNodeRelationshipProperties rel;
419423
}
420424

425+
@Node
421426
static class EntityWithInCorrectRelationshipProperties {
422427
@Id private String id;
423428
@Relationship HasNoTargetNodeRelationshipProperties rel;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ static class UserNode {
298298
@Property(name = "firstName") String first_name;
299299
}
300300

301+
@Node
301302
static class SomeOtherClass {
302303

303304
}
@@ -318,6 +319,7 @@ public void doNothing() {
318319
}
319320
}
320321

322+
@Node
321323
static class BikeNode {
322324

323325
@Id private String id;
@@ -334,6 +336,7 @@ static class BikeNode {
334336
Map<String, Object> funnyDynamicProperties;
335337
}
336338

339+
@Node
337340
static class EnumRelNode {
338341

339342
@Id private String id;
@@ -343,18 +346,21 @@ static class EnumRelNode {
343346
Map<ExtendedA, BikeNode> relEA;
344347
}
345348

349+
@Node
346350
static class TripNode {
347351

348352
@Id private String id;
349353

350354
String name;
351355
}
352356

357+
@Node
353358
static class InvalidId {
354359

355360
@Id @GeneratedValue @Property("getMappingFunctionFor") private String id;
356361
}
357362

363+
@Node
358364
static class InvalidIdType {
359365

360366
@Id @GeneratedValue private String id;

0 commit comments

Comments
 (0)