title: Executing jpa query within a loop gives null
body: <p>I'm trying to run a jpa select query within a loop and add its' result to a list. But I'm getting null.</p>
  
  <p>Here's what I've done : </p>
  
  <pre><code>String[] splittedTag = tagName.split("\\s+");
  
  String getTags;
  Query query;
  
  for(String tag : splittedTag)
  {
      getTags = "select distinct f.tagname from Filemetadata f where f.tagname LIKE '%"+tag+"%'";
      query = entityManager.createQuery(getTags);
      list.addAll(query.getResultList());
  }
  </code></pre>
  
  <p>Later I use patterns to filter my results according the splitted tags and when there's a match in the results with all the splitted tags, I add that result into a jsonArrayBuilder and return it to a calling method.</p>
  
  <pre><code>Pattern pattern = Pattern.compile(stringPattern,Pattern.CASE_INSENSITIVE);
  
  for(int i = 0; i &lt; list.size(); i++){
      if (pattern.matcher((CharSequence) list.get(i)).find()) 
      {
          jsonArrayBuilder.add(list.get(i));
      }
  }
  return jsonArrayBuilder; 
  </code></pre>
  
  <p>Is the query used the right way? Is it possible to be used this way? Help me figure and would greatly appreciate your help and thanks a lot in advance.</p>