DATAGRAPH-1030 - Avoid unconditional registration of entities in Neo4jMappingContext from all scanned classes.

This adds an additional filter that checks the content of Neo4j-OGMs metadata whether a class info is annotated with `NodeEntity` or `RelationshipEntity` before materializing it as en entity in the mapping context, too.
This commit is contained in:
Michael Simons
2020-08-31 15:05:47 +02:00
committed by GitHub
parent 1e4feb0d21
commit 8ad0301753
2 changed files with 48 additions and 1 deletions

View File

@@ -21,9 +21,13 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.metadata.AnnotationsInfo;
import org.neo4j.ogm.metadata.ClassInfo;
import org.neo4j.ogm.metadata.FieldInfo;
import org.neo4j.ogm.metadata.MetaData;
@@ -72,7 +76,15 @@ public class Neo4jMappingContext extends AbstractMappingContext<Neo4jPersistentE
*/
public Neo4jMappingContext(MetaData metaData) {
this.metaData = metaData;
metaData.persistentEntities().stream().filter(k -> k.getUnderlyingClass() != null)
Predicate<ClassInfo> underlyingClassIsPresent = classInfo -> classInfo.getUnderlyingClass() != null;
Predicate<ClassInfo> isAnnotatedEntity = classInfo -> {
final AnnotationsInfo annotationsInfo = classInfo.annotationsInfo();
return annotationsInfo.get(NodeEntity.class) != null || annotationsInfo.get(RelationshipEntity.class) != null;
};
metaData.persistentEntities().stream()
.filter(underlyingClassIsPresent.and(isAnnotatedEntity))
.forEach(k -> addPersistentEntity(k.getUnderlyingClass()));
installDefaultConverter();

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2011-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.mapping;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.neo4j.ogm.metadata.MetaData;
/**
* @author Michael J. Simons
*/
public class Neo4jMappingContextTests {
@Test
public void shouldOnlyAddAnnotatedEntities() {
MetaData metaData = new MetaData(this.getClass().getPackage().getName());
Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(metaData);
assertThat(neo4jMappingContext.hasPersistentEntityFor(this.getClass())).isFalse();
}
}