title: Best way to use hibernate for complex queries like top N per group
body: <p>I'm working now for a while on a reporting applications where I use hibernate to define my queries. However, more and more I get the feeling that for reporting use cases this is not the best approach.</p>
  
  <ol>
  <li>The queries only result partial columns, and thus not typed objects
  (unless you cast all fields in java).</li>
  <li>It is hard to express queries without going straight into sql or
  hql.</li>
  </ol>
  
  <p>My current problem is that I want to get the top N per group, for example the last 5 days per element in a group, where on each day I display the amount of visitors. </p>
  
  <p>The result should look like:</p>
  
  <pre><code>| RowName | 1-1-2009 | 2-1-2009 | 3-1-2009 | 4-1-2009 | 5-1-2009
  | SomeName| 1        | 42       | 34       | 32       | 35
  </code></pre>
  
  <p>What is the best approach to transform the data which is stored per day per row to an output like this? Is it time to fall back on regular sql and work with untyped data?</p>
  
  <p>I really want to use typed objects for my results but java makes my life pretty hard for that. Any suggestions are welcome!</p>