title: HQL for referencing join column in self join
body: <p>Hi I just need a HQL query to reference join column in self join.when i am trying to access join column it is throwing an exception unable to resolve category_id.Thanks in advance.</p>
  
  <pre><code>package com.rst;
  
   import java.util.HashSet;
   import java.util.Set;
  
   import javax.persistence.CascadeType;
   import javax.persistence.Entity;
   import javax.persistence.GeneratedValue;
   import javax.persistence.Id;
   import javax.persistence.JoinColumn;
   import javax.persistence.ManyToOne;
   import javax.persistence.OneToMany;
   import javax.persistence.Table;
  
   import org.hibernate.annotations.GenericGenerator;
   import org.hibernate.annotations.Parameter;
  
   @Entity
   @Table
   public class SubCategory {
  @Id
  @GeneratedValue(generator="Category_Sequence_StyleGenerator")
  
  
  private int subCategory_id;
  
  private String category_name;
  
  @ManyToOne(cascade = {CascadeType.ALL})
  @JoinColumn(name = "category_id")
  private SubCategory category;
  
  @OneToMany(mappedBy = "category")
  private Set&lt;SubCategory&gt; set = new HashSet&lt;SubCategory&gt;();
  
  public SubCategory() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  public SubCategory(String category_name) {
      super();
      this.category_name = category_name;
  }
  
  public String getCategory_name() {
      return category_name;
  }
  
  public void setCategory_name(String category_name) {
      this.category_name = category_name;
  }
  
  public SubCategory getCategory() {
      return category;
  }
  
  public void setCategory(SubCategory category) {
      this.category = category;
  }
  
  public Set&lt;SubCategory&gt; getSet() {
      return set;
  }
  
  public void setSet(Set&lt;SubCategory&gt; set) {
      this.set = set;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my Manager class where i am trying to get all the sub categories of a category by referencing  join column(in my case "category_id").</p>
  
  <pre><code>package com.rst;
  
   import java.util.List;
  
   import org.hibernate.Query;
   import org.hibernate.Session;
   import org.hibernate.SessionFactory;
  
   public class CategoryManager {
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
      SessionFactory sessionFactory;
      try {
          sessionFactory = Util.getSessionFactory();
      }
      catch(Throwable th){
          System.out.println("some error"+th);
          throw new ExceptionInInitializerError();
      }
  
      Session session = sessionFactory.openSession();
  
      Query q = session.createQuery(
                "select s.category_name from SubCategory s where s.category_id = 17");
      List&lt;String&gt; lst = q.list();
      for(String str:lst){
          System.out.println(str);
      }
  }
  </code></pre>
  
  <p>}</p>