DATACMNS-957 - AuditingHandler now works with entities without an identifier.

Entities without an identifier previously an exception because the IsNewStrategyFactory wasn't able to determine a strategy even if there was no auditing to be applied in the first place.

We now eagerly check for auditability and skip the lookup for an IsNewStrategy completely in case that check returns false.

Related pull request: #189.
This commit is contained in:
Oliver Gierke
2016-12-14 13:54:01 +01:00
parent d1d4931504
commit cdbd072083
3 changed files with 18 additions and 1 deletions
@@ -134,6 +134,16 @@ public class AuditingHandler implements InitializingBean {
touch(source, false);
}
/**
* Returns whether the given source is considered to be auditable in the first place
*
* @param source can be {@literal null}.
* @return
*/
protected final boolean isAuditable(Object source) {
return factory.getBeanWrapperFor(source) != null;
}
private void touch(Object target, boolean isNew) {
AuditableBeanWrapper wrapper = factory.getBeanWrapperFor(target);
@@ -73,7 +73,7 @@ public class IsNewAwareAuditingHandler extends AuditingHandler {
*/
public void markAudited(Object object) {
if (object == null) {
if (!isAuditable(object)) {
return;
}
@@ -101,8 +101,15 @@ public class IsNewAwareAuditingHandlerUnitTests extends AuditingHandlerUnitTests
handler.markModified(null);
}
@Test // DATACMNS-957
public void skipsEntityWithoutIdentifier() {
getHandler().markAudited(new EntityWithoutId());
}
static class Domain {
@Id Long id;
}
static class EntityWithoutId {}
}