DATAGRAPH-1423 - Add a better error message when persisting invalid implicit entities.
This commit is contained in:
@@ -48,8 +48,14 @@ final class IdPopulator {
|
||||
Neo4jPersistentEntity<?> nodeDescription = neo4jMappingContext.getRequiredPersistentEntity(entity.getClass());
|
||||
IdDescription idDescription = nodeDescription.getIdDescription();
|
||||
|
||||
if (idDescription == null && nodeDescription.isRelationshipPropertiesEntity()) {
|
||||
return entity;
|
||||
if (idDescription == null) {
|
||||
if (nodeDescription.isRelationshipPropertiesEntity()) {
|
||||
return entity;
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Cannot persist implicit entity due to missing id property on " + nodeDescription.getUnderlyingClass()
|
||||
+ ".");
|
||||
}
|
||||
}
|
||||
|
||||
// Filter in two steps to avoid unnecessary object creation.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.neo4j.repository.event;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
@@ -94,12 +95,24 @@ class IdPopulatorTest {
|
||||
assertThat(populatedSample.theId).isEqualTo("Not necessary unique.");
|
||||
}
|
||||
|
||||
@Test // DATAGRAPH-1423
|
||||
void shouldNotFailWithNPEOnMissingIDGenerator() {
|
||||
|
||||
IdPopulator idPopulator = new IdPopulator(new Neo4jMappingContext());
|
||||
assertThatIllegalStateException().isThrownBy(() -> idPopulator.populateIfNecessary(new ImplicitEntityWithoutId()))
|
||||
.withMessage("Cannot persist implicit entity due to missing id property on " + ImplicitEntityWithoutId.class + ".");
|
||||
}
|
||||
|
||||
@Node
|
||||
static class Sample {
|
||||
|
||||
@Id @GeneratedValue(DummyIdGenerator.class) private String theId;
|
||||
}
|
||||
|
||||
static class ImplicitEntityWithoutId {
|
||||
|
||||
}
|
||||
|
||||
static class DummyIdGenerator implements IdGenerator<String> {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user