java ee - Selecting an entity by collection set equality -


i'm trying jpql query or jpa operation following. have element, consist of element collection of strings:

@entity(name="request") public class request {      @elementcollection     private set<string> keywords;     ...     } 

i want able select entity whos keywords matches given set of strings. i've looked using in match if 1 keyword exists. how match if keywords exist?

the simplest approach can think of 2 count queries:

  1. a count of number of keywords in set
  2. a count of number of keywords not in set

the result of #1 should equal number of keywords in set. result of #2 should equal 0. example:

list<request> requests =    em.createquery("select r request r " +                  "where (select count(k1) request r1 " +                  "       join r1.keywords k1 " +                  "       r1 = r , k1 in :keywords) = :numkeywords " +                  "and (select count(k2) request r2 " +                  "     join r2.keywords k2 " +                  "     r2 = r , k2 not in :keywords) = 0", request.class)      .setparameter("keywords", keywords)      .setparameter("numkeywords", keywords.size())      .getresultlist(); 

if cared whether or not set subset of request's keywords, second count not needed. can done in single query group by:

list<request> requests =        em.createquery("select r request r " +                      "join r.keywords k " +                      "where k in :keywords " +                      "group r " +                      "having count(r) = :numkeywords", request.class)         .setparameter("keywords", keywords)         .setparameter("numkeywords", keywords.size())         .getresultlist(); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -