Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.springframework.context.ApplicationListener;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.data.jdbc.mapping.event.BeforeSaveEvent;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -35,6 +37,7 @@
public class JdbcAuditingEventListener implements ApplicationListener<BeforeSaveEvent> {

@Nullable private AuditingHandler handler;
private JdbcMappingContext context;

/**
* Configures the {@link AuditingHandler} to be used to set the current auditor on the domain types touched.
Expand All @@ -48,6 +51,17 @@ public void setAuditingHandler(ObjectFactory<AuditingHandler> auditingHandler) {
this.handler = auditingHandler.getObject();
}

/**
* Configures a {@link JdbcMappingContext} that use for judging whether new object or not.
* @param context must not be {@literal null}
*/
public void setJdbcMappingContext(JdbcMappingContext context) {

Assert.notNull(context, "JdbcMappingContext must not be null!");

this.context = context;
}

/**
* {@inheritDoc}
*
Expand All @@ -59,11 +73,14 @@ public void onApplicationEvent(BeforeSaveEvent event) {
if (handler != null) {

event.getOptionalEntity().ifPresent(entity -> {

if (event.getId().getOptionalValue().isPresent()) {
handler.markModified(entity);
} else {
@SuppressWarnings("unchecked")
Class<Object> entityType = event.getChange().getEntityType();
JdbcPersistentEntityInformation<Object, ?> entityInformation =
context.getRequiredPersistentEntityInformation(entityType);
if (entityInformation.isNew(entity)) {
handler.markCreated(entity);
} else {
handler.markModified(entity);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.lang.annotation.Annotation;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand Down Expand Up @@ -83,6 +84,7 @@ protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandle
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(listenerClass);
builder.addPropertyValue("auditingHandler",
ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), null));
builder.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
registerInfrastructureBeanWithId(builder.getRawBeanDefinition(), listenerClass.getName(), registry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ interface AuditingAnnotatedDummyEntityRepository extends CrudRepository<Auditing
@Data
static class AuditingAnnotatedDummyEntity {

@Id Long id;
@Id long id;
@CreatedBy String createdBy;
@CreatedDate LocalDateTime createdDate;
@LastModifiedBy String lastModifiedBy;
Expand Down