DATAJDBC-216 - Support @Id property primitive type for auditing.
The new check is now properly done using Spring Data meta Informationen. Original pull request: #71.
This commit is contained in:
committed by
Jens Schauder
parent
46a07ac9f7
commit
0c863eb180
@@ -19,6 +19,8 @@ import org.springframework.beans.factory.ObjectFactory;
|
||||
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;
|
||||
@@ -35,6 +37,7 @@ import org.springframework.util.Assert;
|
||||
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.
|
||||
@@ -48,6 +51,17 @@ public class JdbcAuditingEventListener implements ApplicationListener<BeforeSave
|
||||
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}
|
||||
*
|
||||
@@ -59,11 +73,14 @@ public class JdbcAuditingEventListener implements ApplicationListener<BeforeSave
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.jdbc.repository.config;
|
||||
|
||||
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;
|
||||
@@ -83,6 +84,7 @@ class JdbcAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(listenerClass);
|
||||
builder.addPropertyValue("auditingHandler",
|
||||
ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), null));
|
||||
builder.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
|
||||
registerInfrastructureBeanWithId(builder.getRawBeanDefinition(), listenerClass.getName(), registry);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user