title: hibernate native sql query returned
body: <p>I want to do a native SQL query, which joins 2 Tables.</p>
  
  <p>The return value should be all elements of the District Table, which would be <code>(id, name, district_code, coordinates)</code>, and also a <code>count(*)</code> of all Objects which are in a <code>District</code> (therefore the join with some other table).</p>
  
  <p>So I have all columns of district <code>(district.*)</code> one field which is <code>Count(*)</code>.
  Which kind of query can I use, so that I can comfortable use it in my java code? I can't add an entity or, because the <code>count(*)</code> wouldn't fit to it!?</p>
  
  <p>I have got a District class which looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement
  public class District extends AbstractEntity{
  
      private int id;
      private String name;
      private int district_code;
      @Transient
      private int carsQuantity;
  
      public District(){}
  
      @Override
      public int getId() {
          return id;
      }
  
      @Override
      public void setId(int id) {
          this.id = id;       
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getDistrict_code() {
          return district_code;
      }
  
      public void setDistrict_code(int district_code) {
          this.district_code = district_code;
      }
  
      public int getCarsQuantity() {
          return carsQuantity;
      }
  
      public void setCarsQuantity(int carsQuantity) {
          this.carsQuantity = carsQuantity;
      }
  }
  </code></pre>
  
  <p>My District.hbm.xml:</p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;hibernate-mapping package="at.opendata.entitys"&gt;      
      &lt;class name="District" table="districts"&gt;       
          &lt;id name="id" column="id"&gt;
              &lt;generator class="increment"/&gt;
          &lt;/id&gt;
          &lt;property name="name"/&gt;
          &lt;property name="district_code"/&gt;                
      &lt;/class&gt;    
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I can't do it with HQL oder JPQL, because I need a SUBSELECT in my FROM CLAUSE.</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT d.id, count(*) FROM (SELECT cd.coordinates AS coordinates FROM cars AS c LEFT JOIN cardetail AS cd ON (c.car_id = cd.car_id)) AS c CROSS JOIN districts AS d WHERE ST_CONTAINS(d.coordinates, c.coordinates) GROUP BY id
  </code></pre>