title: org.Hibernate.QueryException: not an association for table id
body: <p>I am new to Hibernate and have been struggling with the hibernate query. I get <code>org.Hibernate.QueryException: not an association: id</code>. </p>
  
  <p>This is the query that works correctly and that I was trying to implement. Lets say I have rooms with shelves with boxes. Each box contains an item. I have only the barcode for the box, and I want to get all items that are in that room.</p>
  
  <pre><code>select * from items where location_id in 
     (select id from storage where parent_id in
     (select id from storage where parent_id in 
     (select id from storage where barcode like 'some_box_barcode_here')))
  </code></pre>
  
  <p>This is how I organized it in JPA (and I get the exception):</p>
  
  <pre><code>DetachedCriteria subCriteriaRoom = DetachedCriteria.forClass(Storage.class, "storageRoom")
              .createAlias("id", "roomId")
              .createAlias("index", "roomIndex")
              .add(Restrictions.eq("roomIndex", barcode));
  subCriteriaRoom.setProjection(Projections.property("roomId"));
  
  DetachedCriteria subCriteriaShelf = DetachedCriteria.forClass(Storage.class, "storageShelf")
              .createAlias("id", "shelfId")
              .createAlias("parent", "shelfParent")
              .add(Restrictions.in("shelfParent", subCriteriaRoom));
  subCriteriaShelf.setProjection(Projections.property("shelfId"));
  
  DetachedCriteria subCriteriaBox = DetachedCriteria.forClass(Storage.class, "storageBox")
              .createAlias("id", "boxId")
              .createAlias("parent", "boxParent")
              .add(Restrictions.in("boxParent", subCriteriaShelf));
  subCriteriaBox.setProjection(Projections.property("boxId"));
  
  Criteria criteria = session.createCriteria(Item.class, "item");
  criteria.createAlias("storage", "storage")
              .add(Subqueries.propertyEq("location", subCriteriaBox));
  
  List list = criteria.list();
  </code></pre>
  
  <p>Some relevant info about the tables:</p>
  
  <p>1.</p>
  
  <pre><code>@Entity
  @Table(name = "storage")
  public class Storage implements DataModel {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
      private long id;
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "parent_id")
      private Storage parent;
  
      @Column(name = "barcode", length = 31, nullable = true)
      private String barcode;
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>@Entity
  @Table(name = "items")
  public class Items implements DataModel {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
      private long id;
  
      @OneToOne(fetch = FetchType.EAGER) //the foreign key to storage.id
      @JoinColumn(name = "location_id")
      private Storage location;
  </code></pre>
  
  <p>Any hint would be much appreciated.</p>
  
  <p>P.S. The reason I need such a huge query is that right now the rooms are being multiplied in the database because of some bug that won't be fixed for a while.</p>