有没有办法使用Criteria API将class Player与层次结构中的Manager连接起来? 我想在我的DAO中找到所有具有给定ID经理的玩家并尝试过这样的事情:
查询:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Player> query = cb.createQuery(Player.class);
Root<Player> root = query.from(Player.class);
Join<Player, PlayerTeam> playerTeamJoin = root.join(Player_.playerTeam);
Join<PlayerTeam, PlayerTeamAssignment> playerTeamAssignmentJoin = playerTeamJoin.join(PlayerTeam_.playerTeamAssignments);
// Desired statement:
Predicate predicate = cb.equal(playerTeamAssignmentJoin.get(ManagerAssignment_.manager), manager);
类层次结构:
@Entity
class Player {
@OneToOne(mappedBy = "player")
PlayerTeam playerTeam;
}
@Entity
class PlayerTeam {
@JoinColumn(name = "PLAYER_ID", referencedColumnName = "ID")
Player player;
@OneToMany(mappedBy = "playerTeam")
Collection<PlayerTeamAssignment> playerTeamAssignments;
}
@Entity
@DiscriminatorColumn(name = "ASSIGNMENT_TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class PlayerTeamAssignment { // It has three implementations
@ManyToOne
@JoinColumn(name = "PLAYER_TEAM_ID", referencedColumnName = "ID")
PlayerTeam playerTeam;
}
@Entity
@DiscriminatorValue("INDIVIDUAL")
public class ManagerAssignment extends PlayerTeamAssignment {
@ManyToOne
@JoinColumn(name = "MANAGER_ID", referencedColumnName = "ID")
Manager manager; // id inside
}
答案 0 :(得分:1)
我已经找到了解决问题的方法。 我不得不使用Path
Join<Player, PlayerTeam> playerTeamJoin = root.join(Player_.playerTeam);
Join<PlayerTeam, PlayerTeamAssignment> playerTeamAssignments = root.join(PlayerTeam_.playerTeamAssignments);
Path managerPath = ((Path) playerTeamAssignments.as(ManagerAssignment.class)).get(ManagerAssignment_.manager));
你可以在谓词中使用它:
Predicate predicate = cb.equal(managerPath.get(Manager_.globalId), managerId);