So I have a Users table, Projects table and User_Roles table. I have one users which is connected to 2 projects, but when I have just one role for him it returns those 2 projects, but if I have 2 roles for him it returns 4 roles (2x2 projects). How do I prevent this
This is my code for returning the list of projects for user
@Override
public List<Project> retrieve(User user) {
Criteria criteria = super.createCriteria();
criteria.addOrder(Order.desc("date"));
criteria.createCriteria("users").add(Restrictions.eq("id", user.getId()));
return (List<Project>) criteria.list();
}
mappings in User class
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Users_Projects",
joinColumns = @JoinColumn(name = "UserID"), inverseJoinColumns = @JoinColumn(name = "ProjectID"))
public List<Project> getProjects() {
return projects;
}
@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@Column(name = "RoleType")
@JoinTable(name = "User_Roles", joinColumns = @JoinColumn(name = "UserID"))
public Set<Role> getRoles() {
return roles;
}
Any suggestions what is the problem here?
tnx