Skip to content

Commit

Permalink
Move off deprecated TemporalType.TIMESTAMP
Browse files Browse the repository at this point in the history
Favour java.time types for auditing by switching from Date to Instant.

See: #3673
Original Pull Request: #3695
  • Loading branch information
christophstrobl committed Dec 20, 2024
1 parent 4d02955 commit 61725bc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

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;
Expand All @@ -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() {
Expand All @@ -67,12 +63,12 @@ public void setCreatedBy(U createdBy) {
@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
Expand All @@ -88,11 +84,11 @@ public void setLastModifiedBy(U lastModifiedBy) {
@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
*
* @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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public boolean hasLimitingParameters() {
public static class JpaParameter extends Parameter {

private final @Nullable Temporal annotation;

@SuppressWarnings("deprecation")
private @Nullable TemporalType temporalType;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

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;
Expand Down Expand Up @@ -54,6 +54,7 @@
* @author Oliver Gierke
* @author Jens Schauder
* @author Krzysztof Krason
* @author Christoph Strobl
*/
@ExtendWith(SpringExtension.class)
@Transactional
Expand Down Expand Up @@ -111,13 +112,13 @@ void shouldAllowUseOfDynamicSpelParametersInUpdateQueries() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 61725bc

Please sign in to comment.