title: Hibernate query by example (from Spring 3)
body: <p>I've made my entity classes <code>Adress</code>, <code>Road</code> and <code>County</code>. A <code>Road</code> is in a <code>County</code> and an <code>Adress</code> in on a <code>Road</code>. I would like to list all the <code>Adresses</code> in a <code>County</code>. Therefore, in my <code>AdressService</code> I say:</p>
  
  <pre><code>public List&lt;Adress&gt; AllAdresses(County county) {
    Adress adress = new Adress();
    Road road = new Road();
    road.setCounty(county);
    adress.setRoad(road);
  
    Example example = Example.create(adress);
    return (List&lt;Adress&gt;) adressDAO.query(Adress.class, example);
  }
  </code></pre>
  
  <p>In my <code>AdressDAO</code> I have query():</p>
  
  <pre><code>public List query(Class&lt;?&gt; c, Example example) {
    return getSession().createCriteria(c).add(example).setMaxResults(100).list();
  }
  </code></pre>
  
  <p>This executes the following query on my database server:</p>
  
  <pre><code>select this_.AdressId as AdressId2_0_, 
         this_.Description as Descript3_2_0_, 
         this_.DescriptionShort as Descript4_2_0_, 
         this_.HouseLetter as HouseLetter2_0_, 
         this_.HouseNr as HouseNr2_0_, 
         this_.RoadId as RoadId2_0_ 
  from tblAdress this_ 
  where (this_.HouseNr=0) 
  limit 100
  </code></pre>
  
  <p>I had expected it to at least include SOME information about my entity <code>County</code>, and an inner join with <code>tblRoad</code>. <code>tblRoad</code> has a primary key <code>roadId</code>, so I expected <code>this_.roadId</code> to be joined with <code>tblRoad.roadId</code>, and I expected <code>tblRoad.countyId</code> to be set to the primary key of <code>County</code>, that is <code>countyId</code>.</p>
  
  <p>Why is the query in this example not built correctly when I use my own entity types? If I only use integers and strings, they work fine, but not entities. How do I make joins like this work with my own entities?</p>