到目前为止,我实现了以下查询:
select distinct count(a.id)
from A a
inner join B b
on a.id=b.b_id
and (
b.name=?
)
/*and (
b.created=(select max(created) from B where b_id = a.id)
) */
where a.x_id=?
我需要实现的是b.created=(select max(created) from B where b_id = a.id)
的一部分。我发现的大多数示例都使用传入参数进行子查询,但我需要使用父表连接。
builder = entityManager.getCriteriaBuilder();
criteria = builder.createQuery();
a = criteria.from(A.class);
criteria
.select(builder.count(a))
.distinct(true);
Join<A, B> b = a.join("b");
b.on(
builder.and(
builder.equal(b.get("name"), "some")
)
);
criteria.where(builder.equal(a.get("x"), x)));