diff --git a/spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/repository/support/DatastoreAuditingEventListener.java b/spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/repository/support/DatastoreAuditingEventListener.java index a31aca1d6cd..00f5fddb27a 100644 --- a/spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/repository/support/DatastoreAuditingEventListener.java +++ b/spring-cloud-gcp-data-datastore/src/main/java/com/google/cloud/spring/data/datastore/repository/support/DatastoreAuditingEventListener.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,9 +16,11 @@ package com.google.cloud.spring.data.datastore.repository.support; +import com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext; import com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent; import org.springframework.context.ApplicationListener; import org.springframework.data.auditing.AuditingHandler; +import org.springframework.data.mapping.PersistentEntity; /** * Auditing event listener that listens for {@code BeforeSaveEvent}. @@ -29,17 +31,35 @@ public class DatastoreAuditingEventListener implements ApplicationListener entities = event.getTargetEntities(); + + if (entities != null) { + entities.forEach(entity -> { + PersistentEntity persistentEntity = + this.mappingContext.getPersistentEntity(entity.getClass()); + + if (persistentEntity != null && persistentEntity.isNew(entity)) { + this.handler.markCreated(entity); + } else { + this.handler.markModified(entity); + } + }); + } } -} +} \ No newline at end of file