title: How to group by column with hibernate, and retrieve all columns
body: <p>I've inherited an application that retrieves some data from an Oracle DB and then transforms it into XML for another application to import. The reasoning behind why it needs to be done in this manner is a bit long winded, but in short we have a database that looks something like this:</p>
  
  <pre><code>ID | CHILD_ID | IRRELEVANT_COLUMN
  1  | 100      | A
  2  | 200      | E
  2  | 200      | B
  3  | 300      | G
  3  | 300      | ZZ
  3  | 300      | WHO_KNOWS_WHAT_MIGHT_END_UP_HERE
  </code></pre>
  
  <p>We only use the values ID &amp; CHILD_ID - previously there was no IRRELEVANT_COLUMN, so each ID would be unique and the following code was used to retrieve the data from the DB:</p>
  
  <pre><code>public static List&lt;RecordInfo&gt; getRecordInfo() {
      List&lt;RecordInfo&gt; recordInfo = null;
      Session session = HibernateUtils.getSessionFactory().openSession();
      try {
          recordInfo = session.createCriteria(RecordInfo.class)
                  .list();
      }
      catch (Exception e) {
          logger.error("Error reading database", e);
      }
      return recordInfo;
  }
  </code></pre>
  
  <p>RecordInfo.java:</p>
  
  <pre><code>public class RecordInfo {
  
      private Long id;
  
      private Event event;
  
      private Integer childId;
  
      //Snip - Public Getters and Setters below.
  }
  </code></pre>
  
  <p>RecordInfo.hbm.xml:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC 
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
      "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.hewhowas.model.RecordInfo" table="NIGHTMARE_TABLE" lazy="false"&gt;
      &lt;id name="id" type="long" column="ID"&gt;
          &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;property name="childId"&gt;
          &lt;column name="CHILD_ID"/&gt;
      &lt;/property&gt;
      &lt;many-to-one name="event" class="com.hewhowas.model.Event" column="CHILD_ID" fetch="join"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p></p>
  
  <p>I've attempted to use Projections to group the data by ID - but then it only retrieves the column ID and nothing else - and I get Cast Exceptions when trying to cast retrieve a RecordInfo object.</p>
  
  <p>Is there any way that I could use Criteria and Projections to return a result set similar to: </p>
  
  <pre><code>ID | CHILD_ID | IRRELEVANT_COLUMN
  1  | 100      | A
  2  | 200      | B
  3  | 300      | WHO_KNOWS_WHAT_MIGHT_END_UP_HERE
  </code></pre>
  
  <p>The information in "IRRELEVANT_COLUMN" is not used in any manner - so the exact records that are retrieved don't impact the application in any way, I just need to make sure that ONLY one record with id of 1 is returned, and only one record with the id of 2 is returned, so on, etc.</p>
  
  <p>Hope there's enough info. Thanks in advance guys :)</p>