mirror of
https://github.com/spring-projects/spring-data-commons.git
synced 2026-05-03 03:34:35 +01:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+7
@@ -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 {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user