如何找到JSON中包含的内容

时间:2019-06-14 07:28:40

标签: java hibernate jpa

我有一个这样的课程。

@Entity
@Table(name = "permissions")
public class Permission {

    public Permission(){}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonIgnore
    @Version
    private int version;


    @JsonIgnore
    @Convert(converter = JpaConverterJson.class)
    @Column(name = "sub_category", columnDefinition = "json")
    private List<Integer> subCategory = new ArrayList<>();

    .
    .
    .


    <Setter Getter>
    ....

并且我像这样将整数列表存储为JSON Type到数据库。

[349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365]作为JSON,我正在使用MYSQL。

我需要查找此列表中包含的所有权限。

例如: 我有permission的10行,如何找到某行中已经存在的350

如果我正在mysql中进行查询,也许像这样。

select * from permission where JSON_SEARCH(sub_category,'$[*]') = 350

entitManager.createQuery(“从权限中选择g”)

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的办法。

我使用这样的查询。

SELECT  DISTINCT permissions.*
FROM permissions, JSON_TABLE(sub_category, "$[*]" COLUMNS(nr INT PATH '$')) as ids  where ids.nr  = 349;

此查询将帮助您找到349JSON中的值为[123,349,350,452,344,67,676,2,4,666,7334,4353453,65,56,745]的位置

  

注意::该查询仅适用于Mysql版本8.x