title: one-to-many: making Hibernate select the reference's id instead of joining it
body: <p>I have two classes stored in my database using Hibernate. Let's call them <code>Container</code> and <code>Item</code>. Item has a one-to-many relation to Container:</p>
  
  <pre><code>@entity(name = "containers")
  public class Container {
      @Id
      @GeneratedValue
      private long id;
  }
  
  @entity(name = "items")
  public class Item {
      @Id
      @GeneratedValue
      private long id;
  
      @ManyToOne
      @JoinColumn(name = "container_id")
      private Container container;
  }
  </code></pre>
  
  <p>I want to select select all for all items the tuple <code>[ (long)item.id, (long)item.container_id ]</code>, but Hibernate seems to insist on retrieving <code>[ (long)item.id, (Container)item.container ]</code>, introducing a useless (and expensive) join.</p>
  
  <p>I tried that criteria query:</p>
  
  <pre><code>Criteria criteria = session.
      createCriteria(Link.class).
      add(Restrictions.isNotNull("container")).
      setProjection(Projections.projectionList().
          add(Projections.id()).
          add(Projections.property("container")));
  </code></pre>
  
  <p>Is there a matching criteria query. Has to be possible without HQL queries or native SQL queries, hasn't it?</p>
  
  <p><strong>Edit 1:</strong> Working HQL query:</p>
  
  <pre><code>session.createQuery("SELECT item.id, item.container.id " +
                      "FROM items AS item " +
                      "WHERE item.container &lt;&gt; NULL")
  </code></pre>
  
  <p><strong>Edit 2:</strong> FetchType.LAZY is not an option.</p>