Reputation: 593
I have a problem to found a solution for spring specifications with list of objects and @ManyToMany relationship. I have employee entity class which have list of skills:
@Entity
public class EmployeeEntity {
@ManyToMany
@JoinTable(name = "employee_skill",
joinColumns = {@JoinColumn(name = "employee_id")},
inverseJoinColumns = {@JoinColumn(name = "skill_id")})
private List<SkillEntity> skills;
}
public class SkillEntity {
@Column(name = "skill_name")
private String skillName;
}
I want to find employees which have skills which are stored in my custom list of skills:
public static Specification<EmployeeEntity> employeeHasSkills(List<String> skills) {
return (root, query, cb) ->{
return root.get("skills").get("skillName").in(skills);
};
}
List of skills has skillNames as String. So I try to map skills for skillName. Ofc this is not working. Any idea? I'm looking for solution without JPA metamodel.
Upvotes: 1
Views: 853
Reputation: 2841
root.get("skills")
returns List<Skill>
. It does not have skillName
.
You should try something like this
public static Specification<EmployeeEntity> employeeHasSkills(List<String> skills) {
return (root, query, cb) ->{
Join<EmployeeEntity, SkillEntity> skill = root.join("skills", JoinType.LEFT);
return skill.get("skillName").in(skills);
};
}
Upvotes: 1