0

我想实现一个带有分页的表格组件。表中的结果由这样的多选查询检索:

SELECT DISTINCT t0.userId,
            t0.userName,
            t1.rolleName
FROM userTable t0
LEFT OUTER JOIN roleTable t1 ON t0.userId = t1.fkUser
WHERE(t0.userType = 'normalUser' AND t1.roleType = 'loginRole')

这个结果我可以通过多选查询得到。

现在对于分页,我必须首先检索总行数。有没有人可以为这个 sql 之一定义条件查询?我失败了,因为子查询不支持多选,而且我不知道如何将此不同的内容放入 count 语句中。

SELECT COUNT(*) FROM
(
   SELECT DISTINCT t0.userId,
                   t0.userName,
                   t1.rolleName
   FROM userTable t0
   LEFT OUTER JOIN roleTable t1 ON t0.userId = t1.fkUser
   WHERE(t0.userType = 'normalUser' AND t1.roleType = 'loginRole')
)

或者

SELECT COUNT(DISTINCT t0.userId || t0.userName || t1.rolleName)
FROM userTable t0
LEFT OUTER JOIN roleTable t1 ON t0.userId = t1.fkUser
WHERE(t0.userType = 'normalUser' AND t1.roleType = 'loginRole')

提前致谢!

顺便提一句。我在 WebSphere AppServer 上使用 OpenJpa

4

2 回答 2

0

以下内容未经测试,但应该可以工作:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> query = builder.createQuery(Long.class);
Root<User> t0 = query.from(User.class);
Join<User, Role> t1 = t0.join("roles", JoinType.LEFT); 
query.select(builder.concat(t0.get(User_.userId), builder.concat(t0.get(User_.userName), t1.get(Role_.rolleName))).distinct(true);
query.where(cb.equal(t0.get("userType"), "normalUser"), cb.equal(t1.get("roleType"), "loginRole"));
TypedQuery<Long> tq = em.createQuery(query); 
于 2013-03-04T09:02:30.883 回答
0

由于已知问题https://jira.spring.io/browse/DATAJPA-1532多选不适用于 repo.findall 方法。我通过将实体管理器自动装配到服务类来处理这个问题。

@Autowired
    EntityManager entityManager;
    public List<?> getResults() throws ParseException 
    {
        //ModelSpecification modelSpecification = new ModelSpecification();
        CriteriaQuery<DAO> query = modelSpecification.getSpecQuery();
        TypedQuery<DAO> typedQuery = entityManager.createQuery(query);
          List<?> resultList = typedQuery.getResultList();
        
        //List<DAO> allData  = entityManager.createQuery(query).getResultList();
        return resultList;
    }

你可以在这里找到工作代码https://github.com/bsridharpatnaik/CriteriaMultiselectGroupBy

于 2020-07-18T03:47:24.953 回答