我正在尝试使用JPA中的标准api创建后续句子(
eclipselink),
很简单问一下某个类别中是否存在某些用户
我想要的句子:
SELECT
CASE
WHEN EXISTS
(SELECT * FROM user WHERE category = ?)
THEN true
ELSE false
END
bind => [10]
我尝试使用此代码:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Boolean.class);
Root root = criteriaQuery.from(tclass);
Subquery subquery = criteriaQuery.subquery(tclass);
Root subroot = subquery.from(tclass);
subquery.select(subroot);
Predicate subPredicate = criteriaBuilder.equal(subroot.get("category"),category);
subquery.where(subPredicate);
Predicate predicateExists = criteriaBuilder.exists(subquery);
Case booleancase = criteriaBuilder.selectCase();
Expression booleanExpression =
booleancase.when(predicateExists,true)
.otherwise(false);
criteriaQuery.select(booleanExpression);
TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
typedQuery.getResultList();
可悲的是我的句子是以下,我想删除最后一个“来自用户”:
SELECT
CASE
WHEN EXISTS
(SELECT ? FROM user t1 WHERE (t1.category = ?))
THEN ?
ELSE ?
END
FROM user t0
bind => [1, 110, true, false]
任何的想法?