title: Hibernate Criteria Projection
body: <p>Well as the question title says, I am trying to make a projection criteria querying only couple of the table attributes.  </p>
  
  <p>So I have a Person Table/class and it has about 40 attributes. I want my criteria to get dynamical number of attributes, lets say 10, 11 or 12 (SQL terms <code>select firstname, lastname from person</code>) and I was doing it like this:</p>
  
  <pre><code>Transaction tx = session.beginTransaction();
  Criteria crit = session.createCriteria(Person.class);
  crit.setCacheable(true);
  ProjectionList projList = Projections.projectionList();
  projList.add(Projections.property("id"));
  Criterias c = null;
   for (int i = 0; i &lt; checked.size(); i++) {
          Attribute attr = checked.elementAt(i);
          switch (attr) {
              case LASTNAME:
                  projList.add(Projections.property("lastName"));
                  c = enumMap.get(attr);
                  if (c.isChanged()) {
                      String tmp = (String) c.getAnswer();
                      tmp = tmp.replace('*', '%');
                      crit.add(Restrictions.like("lastName", tmp));
                      crit.addOrder(Order.asc("lastName"));
                  }
              case ...THE REST .....
              }
      crit.setProjection(projList);
      retList = crit.list();
      tx.commit();
      return retList;
  </code></pre>
  
  <p>and it gives back that the <code>retList</code> elements are not from the <code>Person.class</code>:</p>
  
  <blockquote>
    <p>INFO [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80<br>
    FATAL [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person
    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person</p>
  </blockquote>
  
  <p>Please help, for now I am listing all the 40+ attr, and it takes up querying time and I do not like it. I am looking also an alternative solution which will help me solve this. I read about <code>ResultTransformer</code> but havent found how to use it in my case.</p>