我有以下HQL声明:

select auditRecord from AuditRecordEntity auditRecord where auditRecord.auditAccount.accountId = :accountId


我想将其转换为使用javax.persistence.criteria.CriteriaBuilder,但是不确定该怎么做,非常感谢您的帮助!

CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Object> query = builder.createQuery();
Root<AuditRecordEntity> root = query.from(AuditRecordEntity.class);
// what next?

最佳答案

尝试这个:

CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<AuditRecordEntity> criteriaQuery = criteriaBuilder.createQuery(AuditRecordEntity.class);
Root<AuditRecordEntity> root = criteriaQuery.from(AuditRecordEntity.class);

Predicate predicate = criteriaBuilder.equal(root.get("auditAccount").get("accountId"), accountId);
criteriaQuery.where(predicate);

TypedQuery<AuditRecordEntity> query = em.createQuery(criteriaQuery);

return query.getSingleResult();

10-07 20:08