【发布时间】:2020-04-09 19:22:55
【问题描述】:
我有一个实体,其中有一个 jsonb 类型的内容字段。复杂的 json 存储在该字段中。我需要在此字段上进行选择。我使用标准 API。这样的查询有效:
public Predicate getPredicate(){
Expression<String> function = builder.function("jsonb_extract_path_text",
String.class,
root.<String>get("content"),
builder.literal("unit"),
builder.literal("id")
);
return builder.like(
function,
"%" + value + "%"
);
}
JSON 看起来像这样:
{
"unit":{
"id":"58bd51815744bf06e001b57b",
"name":"my name",
"another":{
"anotherId":"45545454"
}
}
}
问题在于 JSON 的搜索条件可能完全不同。也许有搜索:unit.id = 58bd51815744bf06e001b57b。可能是这样的:unit.another.anotherId = 45545454。 甚至可能是这样:unit.id = 58bd51815744bf06e001b57b and unit.name = "my name"。
我想我可以像这样解决我的问题:
Expression<String> function = builder.function("jsonb_extract_path_text",
String.class,
root.<String>get("content"),
builder.literal("$.unit.id")
);
但由于某种原因,这不起作用。任何想法如何解决这个问题?
【问题讨论】:
标签: java json postgresql hibernate criteria-api