I have an entity as follow:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@TypeDef(name = "JsonbType", typeClass = JsonBinaryType.class)
public class Message {
@Id
@GeneratedValue
private Long id;
private String content;
@Column(columnDefinition = "jsonb")
@Type(type = "JsonbType")
private readTimes UserTimeSet;
}
public class UserTimeSet extend HashSet<UserTime> {
}
public class UserTime implement Serializable {
private String username;
private Date time;
}
Some records are as follow:
id | read_times
----+--------------------------------------
1 | [{"username": "user1", "time": 12312412}, {"username": "user2", "time": 123}]
2 | [{"username": "user2", "time": 713}]
3 | []
4 | []
For saving object to column with Hibernate, I use Hibernate Types project.Now I want to get records there is no user1 in read_times with JPA criteria? the response must be records with id: 2, 3, 4.
Update: I solved with query but can't convert to JPA Criteria:
SELECT * FROM message WHERE jsonb_path_exists("read_times", '$[*] ? (@.username == "user1")')

If using EclipseLink, REGEXP is supported, and will work on Postgres,
You can also use the SQL function to call any SQL specific syntax inside JPQL
An example for Predicates are as follows: Predicate inJsonNumbers = cb .function("jsonb_extract_path_text", String.class, root.get("json"), cb.literal("number")) .in(filter.getJsonNumbers())
ALso, you can write a custom
SQLFunctionfor theCAST(jsonField->'key' AS float)expression and then use that in JPQL.