21
21
import lombok .Value ;
22
22
import lombok .With ;
23
23
24
+ import java .time .OffsetDateTime ;
25
+ import java .time .OffsetTime ;
24
26
import java .util .Arrays ;
25
27
import java .util .Collections ;
26
28
import java .util .List ;
51
53
import org .springframework .data .repository .CrudRepository ;
52
54
import org .springframework .data .repository .PagingAndSortingRepository ;
53
55
import org .springframework .data .repository .query .QueryByExampleExecutor ;
56
+ import org .springframework .lang .NonNull ;
57
+ import org .springframework .lang .Nullable ;
58
+ import org .springframework .util .Assert ;
54
59
55
60
/**
56
61
* Base for testing Redis repository support in different configurations.
@@ -63,6 +68,7 @@ public abstract class RedisRepositoryIntegrationTestBase {
63
68
@ Autowired PersonRepository repo ;
64
69
@ Autowired CityRepository cityRepo ;
65
70
@ Autowired ImmutableObjectRepository immutableObjectRepo ;
71
+ @ Autowired UserRepository userRepository ;
66
72
@ Autowired KeyValueTemplate kvTemplate ;
67
73
68
74
@ BeforeEach
@@ -474,6 +480,24 @@ void shouldProperlyReadNestedImmutableObject() {
474
480
assertThat (loaded .nested ).isEqualTo (nested );
475
481
}
476
482
483
+ @ Test // GH-2677
484
+ void shouldProperlyHandleEntityWithOffsetJavaTimeTypes () {
485
+
486
+ User jonDoe = User .as ("Jon Doe" )
487
+ .expires (OffsetTime .now ().plusMinutes (5 ))
488
+ .lastAccess (OffsetDateTime .now ());
489
+
490
+ this .userRepository .save (jonDoe );
491
+
492
+ User loadedJonDoe = this .userRepository .findById (jonDoe .getName ()).orElse (null );
493
+
494
+ assertThat (loadedJonDoe ).isNotNull ();
495
+ assertThat (loadedJonDoe ).isNotSameAs (jonDoe );
496
+ assertThat (loadedJonDoe .getName ()).isEqualTo (jonDoe .getName ());
497
+ assertThat (loadedJonDoe .getLastAccessed ()).isEqualTo (jonDoe .getLastAccessed ());
498
+ assertThat (loadedJonDoe .getExpiration ()).isEqualTo (jonDoe .getExpiration ());
499
+ }
500
+
477
501
public static interface PersonRepository
478
502
extends PagingAndSortingRepository <Person , String >, CrudRepository <Person , String >,
479
503
QueryByExampleExecutor <Person > {
@@ -519,6 +543,8 @@ public interface CityRepository extends CrudRepository<City, String> {
519
543
520
544
public interface ImmutableObjectRepository extends CrudRepository <Immutable , String > {}
521
545
546
+ public interface UserRepository extends CrudRepository <User , String > { }
547
+
522
548
/**
523
549
* Custom Redis {@link IndexConfiguration} forcing index of {@link Person#lastname}.
524
550
*
@@ -583,4 +609,72 @@ static class Immutable {
583
609
584
610
Immutable nested ;
585
611
}
612
+
613
+ @ RedisHash ("Users" )
614
+ static class User {
615
+
616
+ static User as (@ NonNull String name ) {
617
+ Assert .hasText (name , () -> String .format ("Name [%s] of User is required" , name ));
618
+ return new User (name );
619
+ }
620
+
621
+ private OffsetDateTime lastAccessed ;
622
+
623
+ private OffsetTime expiration ;
624
+
625
+ @ Id
626
+ private final String name ;
627
+
628
+ private User (@ NonNull String name ) {
629
+ this .name = name ;
630
+ }
631
+
632
+ @ Nullable
633
+ public OffsetTime getExpiration () {
634
+ return this .expiration ;
635
+ }
636
+
637
+ @ Nullable
638
+ public OffsetDateTime getLastAccessed () {
639
+ return this .lastAccessed ;
640
+ }
641
+
642
+ public String getName () {
643
+ return this .name ;
644
+ }
645
+
646
+ public User lastAccess (@ Nullable OffsetDateTime dateTime ) {
647
+ this .lastAccessed = dateTime ;
648
+ return this ;
649
+ }
650
+
651
+ public User expires (@ Nullable OffsetTime time ) {
652
+ this .expiration = time ;
653
+ return this ;
654
+ }
655
+
656
+ @ Override
657
+ public boolean equals (Object obj ) {
658
+
659
+ if (this == obj ) {
660
+ return true ;
661
+ }
662
+
663
+ if (!(obj instanceof User that )) {
664
+ return false ;
665
+ }
666
+
667
+ return this .getName ().equals (that .getName ());
668
+ }
669
+
670
+ @ Override
671
+ public int hashCode () {
672
+ return Objects .hash (getName ());
673
+ }
674
+
675
+ @ Override
676
+ public String toString () {
677
+ return getName ();
678
+ }
679
+ }
586
680
}
0 commit comments