title: Join using Hibernate Criteria without using Alias on Restriction
body: <p>I have an object with 70 attributes. For ease of use I created 2 objects, a 'main' object and a 'details' object, with 1:1 relationship based on an auto-generated integer ID. I had a SEARCH screen that allowed searching on any of the main attributes, for which I build Restriction objects for whatever the user typed in. What was nice was that I did this all through iterating through the fields and building criterion - I didn't need ugly code to specifically handle each of the 30 attributes.</p>
  
  <p>Now they want to search on the details fields as well. My previous screen-field-iterating code works perfectly with no changes (the whole reason for making it 'generic'), however I cannot get the JOIN to work to query on details fields.</p>
  
  <pre><code>class House {
      Integer houseID;
      String address;
      . . .
      HouseDetails houseDetails;
  }
  
  class HouseDetails {
      Integer houseID;
      String color;
      . . .
  }
  </code></pre>
  
  <p>I tried to create an alias and add it to the criteria :</p>
  
  <pre><code>criteria.createAlias("houseDetails", "houseDetails");
  </code></pre>
  
  <p>but I get this error :</p>
  
  <pre><code>org.hibernate.QueryException: could not resolve property: color of: House
  </code></pre>
  
  <p>Here's the thing - I know this would work if I prefix my restrictions with the alias name, but I do NOT want to have to know which table (House or HouseDetails) the field comes from. That would ruin all the automatic looping code and create specific code for each field.</p>
  
  <p>Since SQL can do this as long as the column names are unique :</p>
  
  <pre><code>select * from house, housedetails where house.houseID = housedetails.houseID 
  and color = 'blue';
  </code></pre>
  
  <p>I'm wondering how can I get this to work using criteria??</p>
  
  <p>As an aside, but related to this : Is there a way to perform something like Java's introspection on Hibernate HBM.XML mapping files? A number of times I've wanted to do this to solve problems but never found an answer. For the above problem, if I could easily find out which table contained each field, I could add the prefix to the Restriction. Something like this :</p>
  
  <pre><code>// Map of search keys (columns) to searching values
  for ( String key : parms.keySet() ) {
      String val = parms.get(key);
  
      if ( HIBERNATE-SAYS-KEY-IS-FROM-DETAILS-TABLE ) {
          key = "houseDetails." + key;            
      }    
      criteria.add(Restrictions.eq(key,val));
  }
  </code></pre>