Imagine I have optional search parameters that can be null.
Is there a more concise alternative for checking each of them explicitly before adding a jakarta.persistence.criteria.Predicate to some local List?
I mean this almost looks nice, fairly readable (though, Criteria is never really pretty):
import org.springframework.data.jpa.domain.Specification;
//...
private static Specification<User> buildSpecification(FindUserRequestDto userRequestDto) {
return (root, query, criteriaBuilder) -> {
var nameBeginsWith = criteriaBuilder.like(root.get("name"), userRequestDto.getName() + "%");
var ageGreaterThan = criteriaBuilder.greaterThan(root.get("dateOfBirth"), userRequestDto.getDateOfBirth());
var hasEmail = criteriaBuilder.equal(root.join("emailData").get("email"), userRequestDto.getEmail());
var hasPhone = criteriaBuilder.equal(root.join("phoneData").get("phone"), userRequestDto.getPhone());
return criteriaBuilder.and(nameBeginsWith, ageGreaterThan, hasEmail, hasPhone);
But if I introduce an explicit null check for each optional parameter, it'll get uglier, more verbose.
// like so
private static Specification<User> buildSpecification(FindUserRequestDto userRequestDto) {
return (root, query, criteriaBuilder) -> {
var nameBeginsWith = criteriaBuilder.like(root.get("name"), userRequestDto.getName() + "%");
var bornAfter = criteriaBuilder.greaterThan(root.get("dateOfBirth"), userRequestDto.getDateOfBirth());
var hasEmail = criteriaBuilder.equal(root.join("emailData").get("email"), userRequestDto.getEmail());
var hasPhone = criteriaBuilder.equal(root.join("phoneData").get("phone"), userRequestDto.getPhone());
List<jakarta.persistence.criteria.Predicate> predicates = new ArrayList<>();
if (userRequestDto.getName() != null) predicates.add(nameBeginsWith);
if (userRequestDto.getDateOfBirth() != null) predicates.add(bornAfter);
if (userRequestDto.getEmail() != null) predicates.add(hasEmail);
if (userRequestDto.getPhone() != null) predicates.add(hasPhone);
return criteriaBuilder.and(predicates.toArray(new jakarta.persistence.criteria.Predicate[0]));
};
}
You could argue it's a lost cause to try and make Criteria not verbose and ugly. But since I'm not very familiar with Criteria, I may be unaware of something. I wish Jakarta's Predicates had some or() method.
Here are my (simplified) entities:
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Entity
@Getter
@Setter
@Table(name = "\"user\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(mappedBy = "user")
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER,
cascade = CascadeType.ALL, orphanRemoval = true)
private List<EmailData> emailData;
}
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter
@Setter
@Table(name = "email_data")
public class EmailData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
private String email;
}