title: Hibernate - Create alias projection for INNER JOIN query
body: <p>(This is a simplified version of my domain)</p>
  
  <p>I have an entity <code>Product</code> which has a 1-to-N relationship to <code>Connection</code>. The mapping works correctly so that when I need to retrieve connections for a product I just do <code>product.getConnections();</code>.</p>
  
  <p>I need to integrate with a legacy system's GUI which requires that I produce a list on this format: </p>
  
  <pre><code>|PRODUCT_ID|NAME|CONNECTION_ID|NAME|
  |1|Product A|10|Connection A|
  |1|Product A|11|Connection B|
  |1|Product A|12|Connection C|
  </code></pre>
  
  <p>which is basically a result from this query:</p>
  
  <pre><code>SELECT *
  FROM Product P
  INNER JOIN Connection C ON C.PRODUCT_ID = P.PRODUCT_ID
  </code></pre>
  
  <p>I would like to create a property on the <code>Product</code> entity that could be populated with the connection's name so that I can reuse existing logic on my service. I tried to use <code>@Formula</code> but that does not work. It seems like I can achieve this by creating a Projection alias, but I was not successful at that.</p>
  
  <p>We use Hibernate's <code>Criteria</code> and <code>Projection</code> API to perform the query.</p>
  
  <pre><code>Criteria productCriteria = session.createCriteria(Product.class);
  ProjectionList columns = Projections.projectionList();
  
  columns.add(Projections.property("productId"));
  columns.add(Projections.property("name"));
  columns.add(Projections.property("connectionName")); // How to make this work?
  productCriteria.setProjection(columns);
  
  List&lt;Product&gt; prodList = productCriteria.list();
  </code></pre>
  
  <p>These are my entities:</p>
  
  <pre><code>class Product {
      private Long productId;
      private String name;
  
      @OneToMany(mappedBy = "product")
      private List&lt;Connection&gt; connections;
  
      // How to get this populated?
      @Transient 
      private String connectionName;
  
  
  }
  
  class Connection {
      private Long connectionId;
      @Column(name = "PRODUCT_ID")
      private Long productId;
      private String name;
  
      @ManyToOne
      @JoinColumn(name = "PRODUCT_ID", insertable = false, updatable = false)
      private Product product;
  
  }
  </code></pre>