DATACMNS-867 - ReflectionEntityInformation getId(Object) considers potential null values.

This commit is contained in:
Christoph Strobl
2017-01-24 08:24:13 +01:00
committed by Oliver Gierke
parent 6e0596694d
commit bab5ddade9
2 changed files with 6 additions and 1 deletions

View File

@@ -76,7 +76,7 @@ public class ReflectionEntityInformation<T, ID extends Serializable> extends Abs
*/
@SuppressWarnings("unchecked")
public Optional<ID> getId(Object entity) {
return entity == null ? null : Optional.of((ID) ReflectionUtils.getField(field, entity));
return entity == null ? null : Optional.ofNullable((ID) ReflectionUtils.getField(field, entity));
}
/*

View File

@@ -57,6 +57,11 @@ public class ReflectionEntityInformationUnitTests {
assertThat(information.isNew(primitiveId)).isFalse();
}
@Test // DATACMNS-867
public void detectsNewStateForEntityWithNullId() {
assertThat(new ReflectionEntityInformation<>(Sample.class).isNew(new Sample())).isTrue();
}
private static <T> EntityInformation<T, Serializable> getEntityInformation(Class<T> type) {
return new ReflectionEntityInformation<T, Serializable>(type, Id.class);
}