Move off deprecated TemporalType.TIMESTAMP
Favour java.time types for auditing by switching from Date to Instant. See: #3673 Original Pull Request: #3695
This commit is contained in:
committed by
Mark Paluch
parent
6c9c531664
commit
9f36e39a66
@@ -17,13 +17,11 @@ package org.springframework.data.jpa.domain;
|
||||
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.Temporal;
|
||||
import jakarta.persistence.TemporalType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Auditable;
|
||||
@@ -45,14 +43,12 @@ public abstract class AbstractAuditable<U, PK extends Serializable> extends Abst
|
||||
@ManyToOne //
|
||||
private @Nullable U createdBy;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP) //
|
||||
private @Nullable Date createdDate;
|
||||
private @Nullable Instant createdDate;
|
||||
|
||||
@ManyToOne //
|
||||
private @Nullable U lastModifiedBy;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP) //
|
||||
private @Nullable Date lastModifiedDate;
|
||||
private @Nullable Instant lastModifiedDate;
|
||||
|
||||
@Override
|
||||
public Optional<U> getCreatedBy() {
|
||||
@@ -67,12 +63,12 @@ public abstract class AbstractAuditable<U, PK extends Serializable> extends Abst
|
||||
@Override
|
||||
public Optional<LocalDateTime> getCreatedDate() {
|
||||
return null == createdDate ? Optional.empty()
|
||||
: Optional.of(LocalDateTime.ofInstant(createdDate.toInstant(), ZoneId.systemDefault()));
|
||||
: Optional.of(LocalDateTime.ofInstant(createdDate, ZoneId.systemDefault()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreatedDate(LocalDateTime createdDate) {
|
||||
this.createdDate = Date.from(createdDate.atZone(ZoneId.systemDefault()).toInstant());
|
||||
this.createdDate = createdDate.atZone(ZoneId.systemDefault()).toInstant();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,11 +84,11 @@ public abstract class AbstractAuditable<U, PK extends Serializable> extends Abst
|
||||
@Override
|
||||
public Optional<LocalDateTime> getLastModifiedDate() {
|
||||
return null == lastModifiedDate ? Optional.empty()
|
||||
: Optional.of(LocalDateTime.ofInstant(lastModifiedDate.toInstant(), ZoneId.systemDefault()));
|
||||
: Optional.of(LocalDateTime.ofInstant(lastModifiedDate, ZoneId.systemDefault()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
|
||||
this.lastModifiedDate = Date.from(lastModifiedDate.atZone(ZoneId.systemDefault()).toInstant());
|
||||
this.lastModifiedDate = lastModifiedDate.atZone(ZoneId.systemDefault()).toInstant();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,12 @@ import jakarta.persistence.TemporalType;
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Gierke
|
||||
* @deprecated since 4.0. Please use {@literal java.time} types instead.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Documented
|
||||
@Deprecated(since = "4.0", forRemoval = true)
|
||||
public @interface Temporal {
|
||||
|
||||
/**
|
||||
|
||||
@@ -88,6 +88,8 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
|
||||
public static class JpaParameter extends Parameter {
|
||||
|
||||
private final @Nullable Temporal annotation;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private @Nullable TemporalType temporalType;
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,9 +20,9 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -54,6 +54,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
* @author Krzysztof Krason
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Transactional
|
||||
@@ -111,13 +112,13 @@ public abstract class AbstractAuditingViaJavaConfigRepositoriesTests {
|
||||
em.detach(thomas);
|
||||
em.detach(auditor);
|
||||
|
||||
FixedDate.INSTANCE.setDate(new Date());
|
||||
FixedDate.INSTANCE.setDate(Instant.now());
|
||||
|
||||
SampleSecurityContextHolder.getCurrent().setPrincipal(thomas);
|
||||
auditableUserRepository.updateAllNamesToUpperCase();
|
||||
|
||||
// DateTime now = new DateTime(FixedDate.INSTANCE.getDate());
|
||||
LocalDateTime now = LocalDateTime.ofInstant(FixedDate.INSTANCE.getDate().toInstant(), ZoneId.systemDefault());
|
||||
LocalDateTime now = LocalDateTime.ofInstant(FixedDate.INSTANCE.getDate(), ZoneId.systemDefault());
|
||||
List<AuditableUser> users = auditableUserRepository.findAll();
|
||||
|
||||
for (AuditableUser user : users) {
|
||||
|
||||
@@ -15,24 +15,25 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.util;
|
||||
|
||||
import java.util.Date;
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Holds a fixed {@link Date} value to use in components that have no direct connection.
|
||||
* Holds a fixed {@link Instant} value to use in components that have no direct connection.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public enum FixedDate {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private Date fixedDate;
|
||||
private Instant fixedDate;
|
||||
|
||||
public void setDate(Date date) {
|
||||
public void setDate(Instant date) {
|
||||
this.fixedDate = date;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
public Instant getDate() {
|
||||
return fixedDate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user