以下查询的等效JPA CriteriaBuilder是什么?

时间:2020-05-28 17:46:16

标签: spring jpa jpa-2.0 criteria-api

JPA2条件构建器查询的内容是什么?

Select * from Transaction t1 
where t1.version = (
    select max(t2.version)
    from Transaction t2
    where t1.id=t2.id
)

1 个答案:

答案 0 :(得分:3)

类似的东西:

CriteriaQuery<Transaction> query = cb.createQuery(Transaction.class);
Root<Transaction> t1 = query.from(Transaction.class);

Subquery<Integer> maxVersion = query.subquery(Integer.class);
Root<Transaction> t2 = maxVersion.from(Transaction.class);
maxVersion.where(cb.eq(t1.get(Transaction_.id), t2.get(Transaction_.id));
maxVersion.select(cb.max(t2.get(Transaction_.version));

query.where(cb.eq(t1.get(Transaction_.version), maxVersion));
query.select(t1);