以下查询的等效 JPA CriteriaBuilder 是什么?
What will be the equivalent JPA CriteriaBuilder for following query?
接下来的 JPA2 criteriaBuilder 查询是什么?
Select * from Transaction t1
where t1.version = (
select max(t2.version)
from Transaction t2
where t1.id=t2.id
)
大致如下:
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);
接下来的 JPA2 criteriaBuilder 查询是什么?
Select * from Transaction t1
where t1.version = (
select max(t2.version)
from Transaction t2
where t1.id=t2.id
)
大致如下:
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);