I’m rather new to QueryDSL so this may be an easy one. I have the following structure in my database:
______ ___________ _______
| user | | user2item | | item |
|------| |-----------| |-------|
| id | | user_id | | id |
| name | | item_id | | name |
------ ----------- -------
Now i want to get all users that have at least one item in common with a given user. So i tried:
JPASubQuery subQuery = new JPASubQuery().from(user, item).join(user.items, item).where(user.id.eq(myUserId));
return new JPAQuery(entityManager).from(user).where(user.items.in(subQuery.list(item))).list(user);
But this gives me a problem in the ‘in’ clause because the subquery.list() does not return a List<Item> but a ListSubQuery<Item>. Any help would be really appreciated.
EDIT
Thanks Timo, your suggestion works – but unfortunately if you have more than one subqueries of this kind the resulting statement is much slower than:
User myUser = repository.findOne(myUserId);
return new JPAQuery(entityManager)
.from(user)
.join(user.items, item)
.where(item.in(myUser.getItems()))
.list(user);
Try this instead