title: How create JPA query with dynamicly parameters?
body: <p>I have this method:</p>
  
  <pre><code> public List&lt;User&gt; getReport(String name, int age, String sName, Date bd, 
  
       String address, String msg2, int first, int pageSize) {
       List&lt;User&gt; result = new ArrayList&lt;&gt;();
       result = em.createQuery("from User u, User.class)
                      .setFirstResult(first)
                      .setMaxResults(pageSize)
                      .getResultList();
       return result;
      }
  </code></pre>
  
  <p>Now, this method return all data. I need sett dynamicly parameters. I can make this:</p>
  
  <pre><code>result = em.createQuery("from User u WHERE u.age = :age", User.class)
                      .setParameter("age", age)
                      .setFirstResult(first)
                      .setMaxResults(pageSize)
                      .getResultList();
  </code></pre>
  
  <p>But if my <code>age</code> is null - I dont get result. And with another fields.</p>
  
  <p>I need somethign like:</p>
  
  <pre><code>if(age != null){
  em.createQuery("from User u WHERE u.age = :age", User.class)
                          .setParameter("age", age)
  }
  
  if(name!= null){
  em.createQuery("from User u WHERE u.age = :age AND u.name= :name", User.class)
                          .setParameter("age", age)
  }
  </code></pre>
  
  <p>I can format string query like:</p>
  
  <pre><code>String query = "from User u WHER";
  </code></pre>
  
  <p>and them concate this string </p>
  
  <pre><code>if(age != null){
  query = query +"u.age = :age";
  em.createQuery("from User u WHERE u.age = :age", User.class)
                          .setParameter("age", age)
  }
  </code></pre>
  
  <p>But I thing it is bad practice.</p>