DATACMNS-596 - Polishing.
PersistentEntityInformation now falls back to null values for PersistentEntity instances that don't expose an identifier property.
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.data.repository.core.support;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
|
||||
@@ -49,7 +50,14 @@ public class PersistentEntityInformation<T, ID extends Serializable> extends Abs
|
||||
*/
|
||||
@Override
|
||||
public ID getId(T entity) {
|
||||
return (ID) persistentEntity.getPropertyAccessor(entity).getProperty(this.persistentEntity.getIdProperty());
|
||||
|
||||
PersistentProperty<?> property = persistentEntity.getIdProperty();
|
||||
|
||||
if (property == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (ID) persistentEntity.getPropertyAccessor(entity).getProperty(property);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
@@ -50,8 +52,26 @@ public class PersistentEntityInformationUnitTests {
|
||||
assertThat(information.getId(sample), is(5L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-596
|
||||
*/
|
||||
@Test
|
||||
public void returnsNullIfNoIdPropertyPresent() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = context.getPersistentEntity(EntityWithoutId.class);
|
||||
|
||||
PersistentEntityInformation<Object, Serializable> information = new PersistentEntityInformation<Object, Serializable>(
|
||||
entity);
|
||||
assertThat(information.getId(new EntityWithoutId()), is(nullValue()));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
static class EntityWithoutId {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user