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")')