From 320974743404edb276f08ef3f187fe7ba99d5893 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 9 Jul 2019 12:00:52 +0200 Subject: [PATCH] DATAJDBC-390 - Add ordering to RelationalAuditingEventListener to run before other listeners without explicitly specified order. When an event listener is used to set an id before saving it, this ensures the auditing happens before setting the id. If this is not ensured the auditing listener doesn't consider the entity as new and doesn't set created date and created by user. Original pull request: #159. --- .../RelationalAuditingEventListener.java | 20 +++++++++-- ...nableJdbcAuditingHsqlIntegrationTests.java | 36 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/relational/domain/support/RelationalAuditingEventListener.java b/src/main/java/org/springframework/data/relational/domain/support/RelationalAuditingEventListener.java index 927cf453..e8732e1d 100644 --- a/src/main/java/org/springframework/data/relational/domain/support/RelationalAuditingEventListener.java +++ b/src/main/java/org/springframework/data/relational/domain/support/RelationalAuditingEventListener.java @@ -18,6 +18,7 @@ package org.springframework.data.relational.domain.support; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; import org.springframework.data.auditing.IsNewAwareAuditingHandler; import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing; import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; @@ -25,15 +26,23 @@ import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; /** * Spring JDBC event listener to capture auditing information on persisting and updating entities. *

- * An instance of this class gets registered when you apply {@link EnableJdbcAuditing} to your Spring config. + * An instance of this class gets registered when you enable auditing for Spring Data JDBC. * * @author Kazuki Shimizu * @author Jens Schauder * @author Oliver Gierke - * @see EnableJdbcAuditing */ @RequiredArgsConstructor -public class RelationalAuditingEventListener implements ApplicationListener { +public class RelationalAuditingEventListener implements ApplicationListener, Ordered { + + /** + * The order used for this {@link org.springframework.context.event.EventListener}. This ensures that it will run + * before other listeners without a specified priority. + * + * @see org.springframework.core.annotation.Order + * @see Ordered + */ + public static final int AUDITING_ORDER = 100; private final IsNewAwareAuditingHandler handler; @@ -46,4 +55,9 @@ public class RelationalAuditingEventListener implements ApplicationListener { + + AuditingAnnotatedDummyEntity entity = repository.save(new AuditingAnnotatedDummyEntity()); + + assertThat(entity.id).isNotNull(); + + }); + } + /** * Usage looks like this: *

@@ -280,4 +300,20 @@ public class EnableJdbcAuditingHsqlIntegrationTests { return () -> Optional.of("user"); } } + + /** + * An event listener asserting that it is running after {@link AuditingConfiguration#auditorAware()} was invoked and + * set the auditing data. + */ + @Component + static class OrderAssertingEventListener implements ApplicationListener { + + @Override + public void onApplicationEvent(BeforeSaveEvent event) { + + Object entity = event.getEntity(); + assertThat(entity).isInstanceOf(AuditingAnnotatedDummyEntity.class); + assertThat(((AuditingAnnotatedDummyEntity) entity).createdDate).isNotNull(); + } + } }