我有三个实体。我需要构建 Criteria API,如果唯一用户超过 userCount 变量,我可以在其中选择项目。
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Setter private Long id;
}
@Entity
@Table(name = "session")
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Setter private Long id;
@ManyToOne
@JoinColumn(name = "client_id", referencedColumnName = "id")
@Getter @Setter private Client client;
@ManyToOne
@JoinColumn(name = "project_id", referencedColumnName = "id")
@Getter @Setter private Project project;
}
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Setter private Long id;
}
我想选择所有项目,其中唯一用户> = userCount。我在 jpql 中构造查询
@Query("select p from Project p where (select distinct count(cli.id) from Client as cli
join Session sess on sess.client = cli
join Project as proj on proj = sess.project
where proj.id = p.id) >= :userCount")
我写了标准:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = cb.createQuery(Project.class);
Root<Project> projectRoot = cq.from(Project.class);
List<Predicate> predicates = new ArrayList<>();
Subquery<Long> sub = cq.subquery(Long.class);
Root<Client> subRoot = sub.from(Client.class);
Join<Client, Session> sessionClientJoin = subRoot.join("sessions");
Join<Session, Project> sessionProjectJoin = sessionClientJoin.join("project");
sub.select(cb.count(subRoot.get("id"))).distinct(true);
sub.where(cb.equal(projectRoot.get("id"), sessionProjectJoin.get("id")));
predicates.add(cb.greaterThanOrEqualTo(sub, DefaultParamsHolder.NUMBER_OF_USERS));
cq.select(projectRoot);
cq.where(predicates.toArray(new Predicate[0]));
List<Project> project = em.createQuery(cq).getResultList();
这工作正常,但此标准需要客户端类中的会话。
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter @Setter private Long id;
@OneToMany(mappedBy = "client", fetch = FetchType.LAZY)
private List<Session> sessions;
}
这对我很不利。我需要在客户端类中构建没有会话的 Criteria API。我试过这个:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = cb.createQuery(Project.class);
Root<Project> projectRoot = cq.from(Project.class);
List<Predicate> predicates = new ArrayList<>();
Subquery<Long> sub = cq.subquery(Long.class);
Root<Client> subRoot = sub.from(Client.class);
Join<Session, Client> sessionClientJoin = subRoot.join("client");
Join<Session, Project> sessionProjectJoin = sessionClientJoin.join("project");
sub.select(cb.count(subRoot.get("id"))).distinct(true);
sub.where(cb.equal(projectRoot.get("id"), sessionProjectJoin.get("id")));
predicates.add(cb.greaterThanOrEqualTo(sub, DefaultParamsHolder.NUMBER_OF_USERS));
cq.select(projectRoot);
cq.where(predicates.toArray(new Predicate[0]));
List<Project> project = em.createQuery(cq).getResultList();
这行不通。
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [client] on this ManagedType [com.engage.domain.model.statisctic.Client]
如果没有客户端类中的会话,我怎么能做到这一点?