title: How to get selected fields from table using hibernate (I am not using Criteria object but CriteriaQuery object so setProjection solution won't work)
body: <p>I want to get selected fields from table with filters on the top of it.</p>
  
  <p>For example : 
  If I want to filter my results on the basis of an attribute in the table named "created_on", I would do something like <a href="http://example.com/users/?created_on" rel="nofollow noreferrer">http://example.com/users/?created_on</a> = 01-12-2016</p>
  
  <p>I am achieving above task by doing this : </p>
  
  <pre><code>public List&lt;User&gt; searchUser(List&lt;SearchCriteria&gt; params
              , int offset, int limit) {
          Session session = mysqlConnectionFactory.getSession();
          CriteriaBuilder builder = session.getCriteriaBuilder();
          CriteriaQuery&lt;User&gt; query = builder.createQuery(User.class);
          Root r = query.from(User.class);
  
          Predicate predicate = builder.conjunction();
          if (params != null) {
              for (SearchCriteria param : params) {
                  if (param.getOperation().equalsIgnoreCase("&gt;")) {
                      predicate = builder.and(predicate,
                              builder.greaterThanOrEqualTo(r.get(param.getKey()),
                                      param.getValue().toString()));
                  } else if (param.getOperation().equalsIgnoreCase("&lt;")) {
                      predicate = builder.and(predicate,
                              builder.lessThanOrEqualTo(r.get(param.getKey()),
                                      param.getValue().toString()));
                  } else if (param.getOperation().equalsIgnoreCase(":")) {
                      if (r.get(param.getKey()).getJavaType() == String.class) {
                          predicate = builder.and(predicate,
                                  builder.like(r.get(param.getKey()),
                                          "%" + param.getValue() + "%"));
                      } else {
                          predicate = builder.and(predicate,
                                  builder.equal(r.get(param.getKey()), param.getValue()));
                      }
                  }
              }
              query.where(predicate);
          }
  
          List&lt;User&gt; Users = session.createQuery(query)
                  .setFirstResult(offset).setMaxResults(limit).getResultList();
  
          return Users;
      }
  </code></pre>
  
  <p>Now I also wish to incorporate the functionality which will return selected fields for me, how can I do that?</p>