Skip to content

Commit c4859a8

Browse files
committed
Attempt to recreate issue with Optional getters.
See #2176.
1 parent 6a784a7 commit c4859a8

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/test/java/org/springframework/data/jpa/domain/sample/User.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.data.jpa.domain.sample;
1717

18+
import org.springframework.lang.Nullable;
19+
1820
import java.util.Arrays;
1921
import java.util.Date;
2022
import java.util.HashSet;
23+
import java.util.Optional;
2124
import java.util.Set;
2225

2326
import javax.persistence.*;
@@ -96,7 +99,7 @@ public class User {
9699
private String lastname;
97100
private int age;
98101
private boolean active;
99-
@Temporal(TemporalType.TIMESTAMP) private Date createdAt;
102+
@Temporal(TemporalType.TIMESTAMP) @Nullable private Date createdAt;
100103

101104
@Column(nullable = false, unique = true) private String emailAddress;
102105

@@ -326,11 +329,15 @@ public void setManager(User manager) {
326329
this.manager = manager;
327330
}
328331

329-
/**
330-
* @return the createdAt
331-
*/
332-
public Date getCreatedAt() {
333-
return createdAt;
332+
// /**
333+
// * @return the createdAt
334+
// */
335+
// public Date getCreatedAt() {
336+
// return createdAt;
337+
// }
338+
339+
public Optional<Date> getCreatedAt() {
340+
return Optional.ofNullable(createdAt);
334341
}
335342

336343
/**

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,15 @@ void executesFinderWithAfterKeywordCorrectly() {
756756

757757
flushTestUsers();
758758

759-
assertThat(repository.findByCreatedAtAfter(secondUser.getCreatedAt())).containsOnly(thirdUser, fourthUser);
759+
assertThat(repository.findByCreatedAtAfter(secondUser.getCreatedAt().orElseThrow(() -> new RuntimeException("Nothing")))).containsOnly(thirdUser, fourthUser);
760760
}
761761

762762
@Test // DATAJPA-188
763763
void executesFinderWithBeforeKeywordCorrectly() {
764764

765765
flushTestUsers();
766766

767-
assertThat(repository.findByCreatedAtBefore(thirdUser.getCreatedAt())).containsOnly(firstUser, secondUser);
767+
assertThat(repository.findByCreatedAtBefore(thirdUser.getCreatedAt().orElseThrow(() -> new RuntimeException("Nothing")))).containsOnly(firstUser, secondUser);
768768
}
769769

770770
@Test // DATAJPA-180
@@ -1754,6 +1754,19 @@ void findAllByExampleWithEmptyProbe() {
17541754
assertThat(users).hasSize(4);
17551755
}
17561756

1757+
@Test
1758+
void findAllByExampleWithEmptyProbeAndIgnoringNullValues () {
1759+
flushTestUsers();
1760+
1761+
User prototype = new User();
1762+
prototype.setCreatedAt(null);
1763+
1764+
List<User> users = repository
1765+
.findAll(of(prototype, ExampleMatcher.matching().withIgnoreNullValues().withIgnorePaths("age", "createdAt", "active")));
1766+
1767+
assertThat(users).hasSize(4);
1768+
}
1769+
17571770
@Test // DATAJPA-218
17581771
void findAllByNullExample() {
17591772
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)

0 commit comments

Comments
 (0)