문제

I have to query a Message that is in a provided list of Groups and has not been Deactivated by the current user. Here is some pseudo code to illustrate the properties and entities:

class Message {
  private int messageId; 
  private String messageText;
}

class Group {
  private String groupId;
  private int messageId;
}

class Deactivated {
  private String userId;
  private int messageId;
}

Here is an idea of what I need to query for, it's the last AND clause that I don't know how to do (I made up the compound NOT IN expression). Filtering the deactivated messages by userId can result in multiple messageIds, how can I check if that subset of rows does not contain the messageId?

SELECT msg FROM Message msg, Group group, Deactivated unactive
WHERE 
  group.messageId = msg.messageId 
  AND (group.groupId = 'groupA' OR group.groupId = 'groupB' OR ...) 
  AND ('someUserId', msg.messageId) NOT IN (unactive.userId, unactive.messageId)

Note: The ... is there because I don't know the number of groupIds ahead of time. I receive them as a Collection<String> so I'll need to traverse them and add them to the JPQL dynamically.

도움이 되었습니까?

해결책

It seems you are making a cartesian product in your query. You need to have a subquery to reach to your result. you can have a query like this :

 SELECT msg
 FROM Message msg, Group grp
 WHERE msg.id = grp.msgId 
       AND grp.id IN (...)
       AND msg.id NOT IN (SELECT msgId FROM Desactivated WHERE userId = 'uid') 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top