From b8ad5e8b818b2310a7aa288d789646f7c68873b2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 13 Jun 2011 16:24:10 -0700 Subject: [PATCH] DATAJPA-68 - Refactored AuditingEntityListener. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Separated touch(…) methods for updating and creation as some persistence providers might hand in entities with IDs already assigned into a @PrePersist method and thus the isNew() check will fail. --- .../support/AuditingEntityListener.java | 34 ++++++++++++++----- .../AuditingEntityListenerUnitTests.java | 10 +++--- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/domain/support/AuditingEntityListener.java b/src/main/java/org/springframework/data/jpa/domain/support/AuditingEntityListener.java index f9465b1c7..6f4047d8b 100644 --- a/src/main/java/org/springframework/data/jpa/domain/support/AuditingEntityListener.java +++ b/src/main/java/org/springframework/data/jpa/domain/support/AuditingEntityListener.java @@ -106,13 +106,31 @@ public class AuditingEntityListener implements InitializingBean { /** * Sets modification and creation date and auditor on the target object in - * case it implements {@link Auditable}. + * case it implements {@link Auditable} on persist events. * * @param target */ @PrePersist + public void touchForCreate(Object target) { + + touch(target, true); + } + + + /** + * Sets modification and creation date and auditor on the target object in + * case it implements {@link Auditable} on update events. + * + * @param target + */ @PreUpdate - public void touch(Object target) { + public void touchForUpdate(Object target) { + + touch(target, false); + } + + + private void touch(Object target, boolean isNew) { if (!(target instanceof Auditable)) { return; @@ -121,8 +139,8 @@ public class AuditingEntityListener implements InitializingBean { @SuppressWarnings("unchecked") Auditable auditable = (Auditable) target; - T auditor = touchAuditor(auditable); - DateTime now = dateTimeForNow ? touchDate(auditable) : null; + T auditor = touchAuditor(auditable, isNew); + DateTime now = dateTimeForNow ? touchDate(auditable, isNew) : null; Object defaultedNow = now == null ? "not set" : now; Object defaultedAuditor = auditor == null ? "unknown" : auditor; @@ -139,7 +157,7 @@ public class AuditingEntityListener implements InitializingBean { * @param auditable * @return */ - private T touchAuditor(final Auditable auditable) { + private T touchAuditor(final Auditable auditable, boolean isNew) { if (null == auditorAware) { return null; @@ -147,7 +165,7 @@ public class AuditingEntityListener implements InitializingBean { T auditor = auditorAware.getCurrentAuditor(); - if (auditable.isNew()) { + if (isNew) { auditable.setCreatedBy(auditor); @@ -169,11 +187,11 @@ public class AuditingEntityListener implements InitializingBean { * @param auditable * @return */ - private DateTime touchDate(final Auditable auditable) { + private DateTime touchDate(final Auditable auditable, boolean isNew) { DateTime now = new DateTime(); - if (auditable.isNew()) { + if (isNew) { auditable.setCreatedDate(now); if (!modifyOnCreation) { diff --git a/src/test/java/org/springframework/data/jpa/domain/support/AuditingEntityListenerUnitTests.java b/src/test/java/org/springframework/data/jpa/domain/support/AuditingEntityListenerUnitTests.java index ea2230135..85b238fa7 100644 --- a/src/test/java/org/springframework/data/jpa/domain/support/AuditingEntityListenerUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/domain/support/AuditingEntityListenerUnitTests.java @@ -60,7 +60,7 @@ public class AuditingEntityListenerUnitTests { @Test public void doesNotSetAuditorIfNotConfigured() { - listener.touch(user); + listener.touchForCreate(user); assertNotNull(user.getCreatedDate()); assertNotNull(user.getLastModifiedDate()); @@ -79,7 +79,7 @@ public class AuditingEntityListenerUnitTests { listener.setAuditorAware(auditorAware); - listener.touch(user); + listener.touchForCreate(user); assertNotNull(user.getCreatedDate()); assertNotNull(user.getLastModifiedDate()); @@ -100,7 +100,7 @@ public class AuditingEntityListenerUnitTests { listener.setAuditorAware(auditorAware); listener.setModifyOnCreation(false); - listener.touch(user); + listener.touchForCreate(user); assertNotNull(user.getCreatedDate()); assertNotNull(user.getCreatedBy()); @@ -122,7 +122,7 @@ public class AuditingEntityListenerUnitTests { user = new AuditableUser(1L); listener.setAuditorAware(auditorAware); - listener.touch(user); + listener.touchForUpdate(user); assertNull(user.getCreatedBy()); assertNull(user.getCreatedDate()); @@ -139,7 +139,7 @@ public class AuditingEntityListenerUnitTests { listener.setDateTimeForNow(false); listener.setAuditorAware(auditorAware); - listener.touch(user); + listener.touchForCreate(user); assertNotNull(user.getCreatedBy()); assertNull(user.getCreatedDate());