aarbor
aarbor

Reputation: 1534

Spring JPA Query By Example Match One Null Field but Ignore Others

When querying by Example with JPA, is there a way to specify null matching for one column instead of all? It seems like the Null matching can only be applied to all columns.

class Entity {
  private Integer id;
  private String name;
  private Instant createdAt;
  private Instant deletedAt;
  // getters and setters
}

When querying by example, I want to include when deletedAt is actually null in the example, but ignore the null matching on other fields. In the database, the created_at column is a TIMESTAMP WITHOUT TIME ZONE column (postgresql)

Edit: Solutions proposed in Spring data query where column is null aren't applicable since they don't take advantage of querying by example

Upvotes: 4

Views: 3116

Answers (1)

Robert Niestroj
Robert Niestroj

Reputation: 16131

You can try achieve what you want with ExampleMatcher. Specifically ExampleMatcher.includeNullValues() and / or ExampleMatcher.withIgnorePaths("deletedAt")

Try one or both:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
                                               .withIgnorePaths("deletedAt")
                                               .withIncludeNullValues();
Example<Entity> ex = Example.of(obj, exampleMatcher);

Upvotes: 1

Related Questions