所以我一直在努力解决这个问题,但似乎并不是关于如何做到这一点的好文档。我有一个实体RepairMan
,其中包含Skill
个实体的列表。我需要一个可以返回List<RepairMan>
的查询,其技能列表包含所有技能ID。这是实体的样子:
@Entity
@Table(name = "REPAIRMAN")
public class RepairMan implements Serializable
{
private static final long serialVersionUID = 8151638047721448259L;
@SequenceGenerator(name="REPAIRMAN_SEQ", sequenceName="REPAIRMAN_SEQ", allocationSize=1, initialValue=100)
@Id @GeneratedValue(generator="REPAIRMAN_SEQ")
private Long id;
@OneToMany
@JoinTable(name="REPAIRMAN_SKILLS", joinColumns=@JoinColumn(name="REPAIRMAN_ID"), inverseJoinColumns=@JoinColumn(name="SKILL_ID"))
private List<Skill> skills;
....
}
@Entity
@Table(name = "SKILL")
public abstract class Skill implements Serializable
{
private static final long serialVersionUID = -5272849377636005084L;
@SequenceGenerator(name="SKILL_SEQ_GEN", sequenceName="SKILL_SEQ", allocationSize=1, initialValue=100)
@Id
@GeneratedValue(generator="SKILL_SEQ_GEN", strategy=GenerationType.SEQUENCE)
private Long id;
@Column(name="NAME")
private String name;
@Column(name="DESCRIPTION")
private String description;
...
}
这是我想要的签名以及我能够在脑海中解决的问题:
public class RepairManRepositoryImpl extends QueryDslRepositorySupport implements RepairManRepositoryCustom
{
public CompanyInspectorRepositoryImpl()
{
super(RepairMan.class);
}
@Override
public List<RepairMan> getRepairMenByRequiredSkills(List<Long> skillIds)
{
PathBuilder<RepairMan> repairManPath = new PathBuilder<>(RepairMan.class, "repairman");
PathBuilder repairManSkillsPath = repairManPath.get("skills"); // probably wrong
BooleanBuilder hasAllSkills = new BooleanBuilder();
for (Long skillId : skillIds)
{
hasAllSkills.and(repairManSkillsPath.getNumber("id", Long.class).eq(skillId));
}
JPAQuery query = new JPAQuery(getEntityManager())
.from(repairManPath)
//need to join the repairManSkills somehow
.where(hasAllSkills);
return query.list(repairManPath);
}
}
我知道这并不完全有效,而且我知道使用Qclasses会更容易,但出于兼容性原因,我无法进行Qclasses。