我如何使用 JPA 执行此 SQL 查询?
How can I use JPA do this SQL query?
大家好,我尝试在 JPA 中执行此查询 "SELECT SUM(cevecoin) FROM coin where clid="ABC"
,但它不起作用。有人可以帮助我吗?
public Integer SumCoin(String Clid) {
CriteriaBuilder builder =getSession().getCriteriaBuilder();
CriteriaQuery<CoinBean> criteria =builder.createQuery(CoinBean.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
criteria.select(builder.sum(root.<Integer>get("clid")).as(CoinBean.class)).where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult().getCevecoin();
}
试试这个:
CriteriaBuilder builder =getSession().getCriteriaBuilder();
// the type of query criteria must correspond to the result we want to obtain
CriteriaQuery<BigDecimal> criteria = builder.createQuery(BigDecimal.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
// use multiselect and sum the field cevecoin
criteria.multiselect(builder.sum(root.get("cevecoin")))
criteria.where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult();
大家好,我尝试在 JPA 中执行此查询 "SELECT SUM(cevecoin) FROM coin where clid="ABC"
,但它不起作用。有人可以帮助我吗?
public Integer SumCoin(String Clid) {
CriteriaBuilder builder =getSession().getCriteriaBuilder();
CriteriaQuery<CoinBean> criteria =builder.createQuery(CoinBean.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
criteria.select(builder.sum(root.<Integer>get("clid")).as(CoinBean.class)).where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult().getCevecoin();
}
试试这个:
CriteriaBuilder builder =getSession().getCriteriaBuilder();
// the type of query criteria must correspond to the result we want to obtain
CriteriaQuery<BigDecimal> criteria = builder.createQuery(BigDecimal.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
// use multiselect and sum the field cevecoin
criteria.multiselect(builder.sum(root.get("cevecoin")))
criteria.where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult();