gpt4 book ai didi

jpa - 使用 JPA 标准使用标准构建器为 postgres jsonb 列构建谓词

转载 作者:行者123 更新时间:2023-12-03 14:16:53 26 4
gpt4 key购买 nike

我需要将一个谓词添加到我的 JSONB 列的现有谓词列表中。

实体:

@Entity
@Table(name = "a")
@TypeDefs({
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
})
public class EntityA {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a_id_seq")
@SequenceGenerator(sequenceName = "a_id_seq", allocationSize = 1, name = "a_id_seq")
@Column(name = "id")
private long id;

@Column(name = "name")
private String name;

@Column(name = "json")
@Type(type = "jsonb")
private Json json;

private static class Json {
private String name;

private Integer number;

private String random;
}
}

规范:
    public Specification<EntityA> buildSpecification(Filter filter){

return (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();


Expression<String> nameExpression = root.get("name");

Predicate namePredicate = nameExpression.in(filter.getNames());
predicates.add(namePredicate);

//TODO add a predicate for JSONB here


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

我的输入将是 List jsonNames 或 List jsonNumbers,我想使用此输入构建 CriteriaBuilder.In 并获取任何匹配项。

筛选:
@Data
public class Filter {
private List<String> names;

private List<String> jsonNames;

private List<Integer> jsonNumbers;
}

最佳答案

对于 PostgreSQL

Predicate inJsonNumbers = cb
.function("jsonb_extract_path_text",
String.class,
root.get("json"),
cb.literal("number"))
.in(filter.getJsonNumbers())

Predicate inJsonNames = cb
.function("jsonb_extract_path_text",
String.class,
root.get("json"),
cb.literal("name"))
.in(filter.getJsonNames())

关于jpa - 使用 JPA 标准使用标准构建器为 postgres jsonb 列构建谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com