How to get json column as string in postgresql with jpa criteria builder?

Viewed 899

I have a json column in table like:

    @Type(type = "jsonb")
    @Column(name = "json_data", columnDefinition = "json")
    private List<Persion> jsonData = Collections.emptyList();
    public class Persion implements Serializable {
        private String name;

        private int age;

        private String sex;
    }

And I want to query rows contains specific name in json, I have write this code:

    predicates.add(
            criteriaBuilder.like(
                criteriaBuilder.treat(root.get("jsonData"), String.class),
                "%" + name + "%"
        )
    )

But it throws exception:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

Can I just read json as string and then use where condition to query? Any help would be appreciate.

1 Answers
Related