From 3446c457d5d4e91247163553a40d721d8fedf0b7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 18 Nov 2014 16:46:18 +0100 Subject: [PATCH] DATACMNS-596 - Polishing. PersistentEntityInformation now falls back to null values for PersistentEntity instances that don't expose an identifier property. --- .../support/PersistentEntityInformation.java | 10 +++++++++- .../PersistentEntityInformationUnitTests.java | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/PersistentEntityInformation.java b/src/main/java/org/springframework/data/repository/core/support/PersistentEntityInformation.java index 00c9cc3e9..c514000a0 100644 --- a/src/main/java/org/springframework/data/repository/core/support/PersistentEntityInformation.java +++ b/src/main/java/org/springframework/data/repository/core/support/PersistentEntityInformation.java @@ -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 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); } /* diff --git a/src/test/java/org/springframework/data/repository/core/support/PersistentEntityInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/PersistentEntityInformationUnitTests.java index 8fbf4ceb0..443a31a45 100644 --- a/src/test/java/org/springframework/data/repository/core/support/PersistentEntityInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/PersistentEntityInformationUnitTests.java @@ -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 entity = context.getPersistentEntity(EntityWithoutId.class); + + PersistentEntityInformation information = new PersistentEntityInformation( + entity); + assertThat(information.getId(new EntityWithoutId()), is(nullValue())); + } + static class Sample { @Id Long id; } + + static class EntityWithoutId { + + } }