Skip to content

Commit b67f551

Browse files
christophstroblmp911de
authored andcommitted
Move off deprecated TemporalType.TIMESTAMP
Favour java.time types for auditing by switching from Date to Instant. See: #3673 Original Pull Request: #3695
1 parent b4e9f5d commit b67f551

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/domain/AbstractAuditable.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
import jakarta.persistence.ManyToOne;
1919
import jakarta.persistence.MappedSuperclass;
20-
import jakarta.persistence.Temporal;
21-
import jakarta.persistence.TemporalType;
2220

2321
import java.io.Serializable;
22+
import java.time.Instant;
2423
import java.time.LocalDateTime;
2524
import java.time.ZoneId;
26-
import java.util.Date;
2725
import java.util.Optional;
2826

2927
import org.springframework.data.domain.Auditable;
@@ -45,14 +43,12 @@ public abstract class AbstractAuditable<U, PK extends Serializable> extends Abst
4543
@ManyToOne //
4644
private @Nullable U createdBy;
4745

48-
@Temporal(TemporalType.TIMESTAMP) //
49-
private @Nullable Date createdDate;
46+
private @Nullable Instant createdDate;
5047

5148
@ManyToOne //
5249
private @Nullable U lastModifiedBy;
5350

54-
@Temporal(TemporalType.TIMESTAMP) //
55-
private @Nullable Date lastModifiedDate;
51+
private @Nullable Instant lastModifiedDate;
5652

5753
@Override
5854
public Optional<U> getCreatedBy() {
@@ -67,12 +63,12 @@ public void setCreatedBy(U createdBy) {
6763
@Override
6864
public Optional<LocalDateTime> getCreatedDate() {
6965
return null == createdDate ? Optional.empty()
70-
: Optional.of(LocalDateTime.ofInstant(createdDate.toInstant(), ZoneId.systemDefault()));
66+
: Optional.of(LocalDateTime.ofInstant(createdDate, ZoneId.systemDefault()));
7167
}
7268

7369
@Override
7470
public void setCreatedDate(LocalDateTime createdDate) {
75-
this.createdDate = Date.from(createdDate.atZone(ZoneId.systemDefault()).toInstant());
71+
this.createdDate = createdDate.atZone(ZoneId.systemDefault()).toInstant();
7672
}
7773

7874
@Override
@@ -88,11 +84,11 @@ public void setLastModifiedBy(U lastModifiedBy) {
8884
@Override
8985
public Optional<LocalDateTime> getLastModifiedDate() {
9086
return null == lastModifiedDate ? Optional.empty()
91-
: Optional.of(LocalDateTime.ofInstant(lastModifiedDate.toInstant(), ZoneId.systemDefault()));
87+
: Optional.of(LocalDateTime.ofInstant(lastModifiedDate, ZoneId.systemDefault()));
9288
}
9389

9490
@Override
9591
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
96-
this.lastModifiedDate = Date.from(lastModifiedDate.atZone(ZoneId.systemDefault()).toInstant());
92+
this.lastModifiedDate = lastModifiedDate.atZone(ZoneId.systemDefault()).toInstant();
9793
}
9894
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/Temporal.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
*
3131
* @author Thomas Darimont
3232
* @author Oliver Gierke
33+
* @deprecated since 4.0. Please use {@literal java.time} types instead.
3334
*/
3435
@Retention(RetentionPolicy.RUNTIME)
3536
@Target(ElementType.PARAMETER)
3637
@Documented
38+
@Deprecated(since = "4.0", forRemoval = true)
3739
public @interface Temporal {
3840

3941
/**

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaParameters.java

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public boolean hasLimitingParameters() {
8888
public static class JpaParameter extends Parameter {
8989

9090
private final @Nullable Temporal annotation;
91+
92+
@SuppressWarnings("deprecation")
9193
private @Nullable TemporalType temporalType;
9294

9395
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/config/AbstractAuditingViaJavaConfigRepositoriesTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import jakarta.persistence.EntityManager;
2222

23+
import java.time.Instant;
2324
import java.time.LocalDateTime;
2425
import java.time.ZoneId;
25-
import java.util.Date;
2626
import java.util.List;
2727
import java.util.Optional;
2828
import java.util.concurrent.TimeUnit;
@@ -54,6 +54,7 @@
5454
* @author Oliver Gierke
5555
* @author Jens Schauder
5656
* @author Krzysztof Krason
57+
* @author Christoph Strobl
5758
*/
5859
@ExtendWith(SpringExtension.class)
5960
@Transactional
@@ -111,13 +112,13 @@ void shouldAllowUseOfDynamicSpelParametersInUpdateQueries() {
111112
em.detach(thomas);
112113
em.detach(auditor);
113114

114-
FixedDate.INSTANCE.setDate(new Date());
115+
FixedDate.INSTANCE.setDate(Instant.now());
115116

116117
SampleSecurityContextHolder.getCurrent().setPrincipal(thomas);
117118
auditableUserRepository.updateAllNamesToUpperCase();
118119

119120
// DateTime now = new DateTime(FixedDate.INSTANCE.getDate());
120-
LocalDateTime now = LocalDateTime.ofInstant(FixedDate.INSTANCE.getDate().toInstant(), ZoneId.systemDefault());
121+
LocalDateTime now = LocalDateTime.ofInstant(FixedDate.INSTANCE.getDate(), ZoneId.systemDefault());
121122
List<AuditableUser> users = auditableUserRepository.findAll();
122123

123124
for (AuditableUser user : users) {

spring-data-jpa/src/test/java/org/springframework/data/jpa/util/FixedDate.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@
1515
*/
1616
package org.springframework.data.jpa.util;
1717

18-
import java.util.Date;
18+
import java.time.Instant;
1919

2020
/**
21-
* Holds a fixed {@link Date} value to use in components that have no direct connection.
21+
* Holds a fixed {@link Instant} value to use in components that have no direct connection.
2222
*
2323
* @author Thomas Darimont
24+
* @author Christoph Strobl
2425
*/
2526
public enum FixedDate {
2627

2728
INSTANCE;
2829

29-
private Date fixedDate;
30+
private Instant fixedDate;
3031

31-
public void setDate(Date date) {
32+
public void setDate(Instant date) {
3233
this.fixedDate = date;
3334
}
3435

35-
public Date getDate() {
36+
public Instant getDate() {
3637
return fixedDate;
3738
}
3839
}

0 commit comments

Comments
 (0)