有没有办法用JPA2 CriteriaBuilder
写下以下查询的等价物?
select distinct (a.project_id,c.json) from object_metadata a, project b, (select * from task t where t.queued = (select max(queued) from task t2 where t2.project_id=t.project_id)) c where a.implementation = 'localfs'
and c.project_id = a.project_id;
我写了以下查询,但结果不一样:
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<Tuple> cq= cb.createTupleQuery();
Root<ObjectMetadata> root = cq.from(ObjectMetadata.class);
Path<Task> task = root.get("task");
Expression<org.joda.time.LocalDateTime> queued_ = task.get(Task_.queued);
Expression<String> sJSON = task.get(Task_.sJson);
Path<Project> project = root.get("project");
Expression<String> projectId = project.get("id");
Expression<String> projectName = project.get("name");
Subquery<org.joda.time.LocalDateTime> subquery = cq.subquery(org.joda.time.LocalDateTime.class);
Root<Task> subRoot = subquery.from(Task.class);
Path<Project> subProject = subRoot.get("project");
Expression<String> subProjectId = subProject.get("id");
Expression<org.joda.time.LocalDateTime> queued = subRoot.get(Task_.queued);
subquery.select(cb.greatest(queued));
subquery.where(cb.equal(subProjectId, projectId));
cq.multiselect(projectId.alias("ID"), projectName.alias("NAME"), sJSON.alias("JSON")).distinct(true);
cq.where(cb.and(cb.equal(root.get(ObjectMetadata_.implementation), "localfs"),cb.equal(queued_, subquery)));
TypedQuery<Tuple> tq = getEm().createQuery(cq);