如何在 spring 引导规范上按年龄查询

How to query by age on spring boot specification

@Entity
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "full_name", nullable = false, length = 50)
    private String fullName;
    @Column(name = "current_location", nullable = false)
    private String currentLocation;
    @Column(name = "gender", nullable = false, length = 6)
    private String gender;
    @Column(name = "birth_date", nullable = false)
    private Timestamp birthDate;
}

我使用以下内容按性别过滤用户

public class SearchSpecification implements Specification<Users> {
    private List<SearchCriteria> list;
    public SearchSpecification() {
        this.list = new ArrayList<>();
    }
    public void add(SearchCriteria criteria) {
        list.add(criteria);
    }
    @Override
    public Predicate toPredicate(Root<UserActualDatum> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

           predicates.add(builder.like(root.<String>get(criteria.getKey()), (String) criteria.getValue()));
           return builder.and(predicates.toArray(new Predicate[0]));
    }
}

但我想按年龄过滤用户,我有 birthDate 列存储用户的出生日期。可以使用 AGE() 函数在 postgres 数据库中计算年龄,但是当我进入 Spring 规范查询时,我无法从出生日期计算年龄并通过计算年龄进行过滤。

我添加了年龄列,我们可以不用任何计算得到年龄

@Entity
public class Users {
    ...
    @Formula("date_part('year',AGE(current_date,birth_date))")
    @Column(name = "age")
    private Integer age;
}

在 Postgres 上创建计算年龄的函数

CREATE OR REPLACE FUNCTION get_age( birthday timestamp )
RETURNS integer
AS $CODE$
BEGIN
    RETURN date_part('year',age(birthday));
END
$CODE$
LANGUAGE plpgsql IMMUTABLE;

我修改了 TABLE 通过添加年龄列,该列将根据 birth_date

计算得出
    CREATE TABLE Users (
        ....
        birth_date timestamp not null,
        age text GENERATED ALWAYS AS (get_age(birth_date)) stored 
    );

现在我可以按 age 过滤,age 作为从 birth_date

派生的整数值

当我们开始时 Spring 引导规范查询现在这个工作

public class SearchSpecification implements Specification<Users> {
    private List<SearchCriteria> list;
    public SearchSpecification() {
        this.list = new ArrayList<>();
    }
    public void add(SearchCriteria criteria) {
        list.add(criteria);
    }
    @Override
    public Predicate toPredicate(Root<UserActualDatum> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

                predicates.add(builder.lessThanOrEqualTo(
                        root.<Integer>get(criteria.getKey()), (Integer) criteria.getValue()));

           return builder.and(predicates.toArray(new Predicate[0]));
    }
}

资源: