【发布时间】: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