jsonb - Spring搜索

时间:2018-06-03 13:22:27

标签: spring jpa domain-driven-design jsonb

如何运行查询

  

select * from companies where data->>'inn' like '%12%'

使用org.springframework.data.jpa.domain.Specification

class CompanySpecification : Specification<Company>{
    override fun toPredicate(root: Root<Company>, query:CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder): Predicate? {
        return criteriaBuilder.like(root.get("data-->inn"), "%12%")
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

class CompanySpecification : Specification<Company> {

    override fun toPredicate(root: Root<Company>, query: CriteriaQuery<*>, builder: CriteriaBuilder): Predicate {
        return builder.like(builder.function("jsonb_extract_path_text", String::class.java, root.get<String>("data"), builder.literal("inn")), "12%")
    }

}