【问题标题】:Querydsl: innerJoin and JPASubQueryQuerydsl:innerJoin 和 JPASubQuery
【发布时间】:2015-08-06 14:53:51
【问题描述】:

如何使用 Querydsl 实现这个 SQL 查询?

SELECT UserProfile.* FROM UserAccount_has_Workgroup 
INNER JOIN UserAccount ON UserAccount_has_Workgroup.userAccountID=UserAccount.userAccountID 
INNER JOIN UserProfile ON UserAccount.userAccountID = UserProfile.userAccountID
WHERE UserAccount_has_Workgroup.UserAccountID NOT IN
    (SELECT UserAccountID FROM UserAccount_has_Workgroup WHERE workgroupID = 2)
AND UserAccount.roleId <> 2;

我试过这个,但它返回一个空结果:

JPAQuery query = new JPAQuery(entityManager); 

query = query.from(userAccountHasWorkgroup)
     .innerJoin(userAccountHasWorkgroup.userAccountByUserAccountId, userAccount)
     .innerJoin(userAccountHasWorkgroup.userAccountByUserAccountId.userProfile, qUserProfile)
    .where(userAccountHasWorkgroup.userAccountByUserAccountId
       .notIn(new JPASubQuery().from(userAccountHasWorkgroup)
                .where(userAccountHasWorkgroup.workgroup.id.eq(workgroupId))
                .list(userAccountHasWorkgroup.userAccountByUserAccountId))
            )
   .where(userAccountHasWorkgroup.userAccountByUserAccountId.role.name.ne(role.getRoleString()))
        ;

  List<UserProfile>  results = query.list(userProfile);

【问题讨论】:

  • 查询的构造结果为:“select userAccountHasWorkgroup from UserAccountHasWorkgroup userAccountHasWorkgroup inner join userAccountHasWorkgroup.userAccountByUserAccountId as userAccount inner join userAccountHasWorkgroup.userAccountByUserAccountId.userProfile as userProfile where userAccountHasWorkgroup.userAccountByUserAccountId not in (select userAccountHasWorkgroup. userAccountByUserAccountId 来自 UserAccountHasWorkgroup userAccountHasWorkgroup where userAccountHasWorkgroup.workgroup.id = ?1) 和 userAccountHasWorkgroup.userAccountByUserAccountId.role.name ?2"
  • 我认为错误在于查询的翻译:“select userAccountHasWorkgroup”应该是“select userProfile”

标签: jakarta-ee querydsl


【解决方案1】:

在您的示例中,WHERE 表达式看起来完全不同:

AND UserAccount.roleId <> 2;

对

 where(userAccountHasWorkgroup.userAccountByUserAccountId.role.name.ne(role.getRoleString()))

另外我认为你不应该连续使用两个 WHERE 表达式,但应该

.where(expr1.and(expr2))

类似于原生 SQL

【讨论】:

  • 你说得对,为了简单起见,我省略了一个表格。 where(userAccountHasWorkgroup.userAccountByUserAccountId.role.roleId.ne(roleId)。也一样
  • 如何解开 where 表达式的链接?
  • 您能举个例子吗?
  • 在你的情况下 t 将是 .where( userAccountHasWorkgroup.userAccountByUserAccountId .notIn(new JPASubQuery().from(userAccountHasWorkgroup).where(userAccountHasWorkgroup.workgroup.id.eq(workgroupId)).list(userAccountHasWorkgroup .userAccountByUserAccountId)), userAccountHasWorkgroup.userAccountByUserAccountId.role.name.ne(role.getRoleString()))
  • 在你的情况下 t 将是.where( userAccountHasWorkgroup.userAccountByUserAccountId .notIn(new JPASubQuery().from(userAccountHasWorkgroup) .where(userAccountHasWorkgroup.workgroup.id.eq(workgroupId)) .list(userAccountHasWorkgroup.userAccountByUserAccountId)), userAccountHasWorkgroup.userAccountByUserAccountId.role.name.ne(role.getRoleString()) )
【解决方案2】:

尝试这个查询,显然结果是我所期望的::

JPAQuery query = new JPAQuery(entityManager); 

query = query.from(userAccount)     
        .innerJoin(userAccount.userProfile, userProfile)
        .where(userAccount.notIn(new JPASubQuery()
            .from(userAccountHasWorkgroup).where(userAccountHasWorkgroup.workgroup.id.eq(workgroupId))
            .list(userAccountHasWorkgroup.userAccountByUserAccountId)))
        .where(userAccount.role.name.ne(role.getRoleString()));

从UserAccount 表传递(UserAccount 与N&lt;--&gt;N 有关系UserAccountHasWorkgroup)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多