I am using Spring with Hibernate in my project. I want to get users by a particular role using Hibernate criteria. My User model is connected with Role model using @ManyToMany
annotation:
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinTable(
name = "user_role",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = @JoinColumn(name = "role_id")
)
public Set<Role> getRoles() {
return roles;
}
and here is my criteria in my UserDaoHibernate
:
public List<User> getUsersByRole(List<Role> roles) {
List users = getSession().createCriteria(User.class)
.add(Restrictions.in("roles", roles)).list();
return users;
}
I am getting DataIntegrityViolationException when executing this criteria. Am i missing something or is there any correct way of getting users by their roles?