JPA Query DSL 3.7:在“ NOT IN”子句中添加多个列

时间:2019-07-09 19:17:31

标签: java querydsl

我是queryDsl的新手,需要创建一个查询,其中要考虑“ NOT IN”子句中的两列。

这是我要实现的mysql查询:

SELECT 
*
FROM
  organization.ORG_ROLE
    WHERE
   (ORGANIZATION_ID IN ('test')
    OR ORGANIZATION_ID LIKE 'test1')
    AND (ROLE_ID,ORGANIZATION_ID) NOT IN (SELECT 
        ROLE_ID,ORGANIZATION_ID
    FROM
        ORG_USER_ROLE
    WHERE
        USER_ID LIKE 'ankur_bisht');

这是我试图使此查询正常工作的Java代码:

    Expression<?>[] projection = { qUserRole.roleId, qUserRole.organizationId };

    JPASubQuery innerQuery = new JPASubQuery().from(qUserRole).where(qUserRole.userId.eq(userId));

    JPQLQuery jPQLQuery2 = query.from(qRole).where(qRole.isActive.eq(true)
            .and((qRole.organizationId.in("test")
                    .or(qRole.roleType.roleTypeId.equalsIgnoreCase("test1"))))
            .and(Expressions.list(qRole.roleId, qRole.organizationId).notIn(innerQuery.list(projection))));

但是查询失败。

这是最终查询:

SELECT (ALL columns) FROM ORG_ROLE t0 
WHERE (((t0.IS_ACTIVE = ?) AND ((t0.ORGANIZATION_ID = ?) OR (LOWER(t0.ROLE_TYPE_ID) = ?))) 
AND 
(ORG_ROLE.ROLE_ID, ORG_ROLE.ORGANIZATION_ID) 
NOT IN (SELECT t1.ROLE_ID, t1.ORGANIZATION_ID FROM ORG_USER_ROLE t1 
WHERE (t1.USER_ID = ?)))

我正在(ORG_ROLE.ROLE_ID,ORG_ROLE.ORGANIZATION_ID)

中丢失别名值

这将引发以下错误:

  

RootCause:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'IN / ALL / ANY子查询'中的未知列'ORG_ROLE.ROLE_ID'

0 个答案:

没有答案