I need to query an object that contains a list.
@Entity
@Table(name = "userAccount")
public class UserAccounts implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "email", nullable = false, unique = true)
private String username;
@Column(name = "hash", nullable = false, length = 60)
private String hash;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "userID"), inverseJoinColumns = @JoinColumn(name = "roleID"))
private List<Role> roles;
@Enumerated(EnumType.ORDINAL)
private Status status;
//getters and setters
}
I need to get objects that contains role "ROLE_USER". how can I achieve that using CriteriaBuilder?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserAccounts> cq = cb.createQuery(UserAccounts.class);
Root<UserAccounts> ua = cq.from(UserAccounts.class);
What Should I put here?
// cq.where(cb.and(cq.equals(ua.get("roles")),
// cq.equals(ua.get("status"), Status.ACTIVE)));
TIA.