title: Hibernate 5.x migration Criteria to CriteriaBuilder
body: <p>I have a table modeled in a legacy .hbm.xml file.</p>
  
  <p>The legacy code to retrieve a row uses an org.hibernate.Criteria to get a uniqueResult(). In migrating to Hibernate 5.x, Criteria is deprecated so I am trying to use CriteriaBuilder to achieve the same. However when I try to add restrictions (Hib 5.x) based on what worked previously (Hib 4.x) I get an IllegalArgumentException:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [xyzKey.plantName] on this ManagedType [com.foo.bar.Plant]
  at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:147)
  at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:118)
  at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:43)
  at org.hibernate.query.criteria.internal.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:111)
  at org.hibernate.query.criteria.internal.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204)
  at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:177)
  </code></pre>
  
  <p>Plant.hbm.xml:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class lazy="false" name="com.foo.bar.Plant" table="Plant"&gt;
          &lt;meta inherit="false" attribute="extends"&gt;com.foo.bar.PlantBase&lt;/meta&gt;
  
          &lt;id name="id" type="integer" column="plantID" unsaved-value="null"&gt;
              &lt;meta inherit="false" attribute="scope-set"&gt;protected&lt;/meta&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
  
          &lt;component name="xyzKey" class="com.foo.bar.PlantKey"&gt;
              &lt;meta inherit="false" attribute="use-in-tostring"&gt;true&lt;/meta&gt;
              &lt;property name="plantName" type="string" index="xyzKeyndx" unique-key="plantKey"&gt;
                  &lt;column name="plantName" /&gt;
              &lt;/property&gt;
              &lt;property name="xyzRevision" type="string" index="xyzKeyndx" unique-key="plantKey"&gt;
                  &lt;column name="xyzRevision" length="100"/&gt;
              &lt;/property&gt;
          &lt;/component&gt;
  
          &lt;property name="active" type="java.lang.Boolean" index="xyzKeyndx"&gt;
              &lt;column name="active"  not-null="true"/&gt;
          &lt;/property&gt;
  
          &lt;property name="description" type="string" not-null="false"&gt;
              &lt;meta inherit="false" attribute="field-description"&gt;User specified description. Does not need to be unique.&lt;/meta&gt;
              &lt;meta inherit="false" attribute="use-in-tostring"&gt;false&lt;/meta&gt;
              &lt;column name="descr" not-null="false" /&gt;
          &lt;/property&gt;
  
          &lt;property name="location" type="string" not-null="true"&gt;
              &lt;column name="location" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Hibernate 4.x based code that works:</p>
  
  <pre><code>protected Plant selectPlant(Session session, PlantKey xyzKey)
  {
      Criteria c = session.createCriteria(Plant.class);
      if (Util.isEmpty(xyzKey.getXyzRevision()))
      {
          SimpleExpression plantName = Restrictions.eq("xyzKey.plantName", xyzKey.getPlantName());
          SimpleExpression active = Restrictions.eq("active", true);
          c.add(Restrictions.and(plantName, active));
      }
      else
      {
          c.add( Restrictions.eq("xyzKey", xyzKey) );
      }
      Plant plant = (Plant)c.uniqueResult();
      return plant;
  }
  </code></pre>
  
  <p>Hibernate 5.x based code that fails:</p>
  
  <pre><code>protected Plant selectPlant(Session session, PlantKey xyzKey)
  {
      ElapsedTimer timer = new ElapsedTimer();
  
      CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
      CriteriaQuery&lt;Plant&gt; criteriaQuery = criteriaBuilder.createQuery(Plant.class);
      Root&lt;Plant&gt; root = criteriaQuery.from(Plant.class);
  
      List&lt;Predicate&gt; restrictions = new ArrayList&lt;&gt;();
      if ( FBCUtil.isEmpty(vpeKey.getVpeRevision()) )
      {
          restrictions.add(criteriaBuilder.equal(root.get("xyzKey.plantName"), xyzKey.getPlantName())); // FAILS HERE!
          restrictions.add(criteriaBuilder.equal(root.get("active"), true));
      }
      else
      {
          restrictions.add(criteriaBuilder.equal(root.get("xyzKey"), xyzKey));
      }
  
      criteriaQuery.where(restrictions.toArray(new Predicate[restrictions.size()]));
  
      Query&lt;Plant&gt; query = session.createQuery(criteriaQuery);
      Plant plant = query.uniqueResult();
  
      return plant;
  }
  </code></pre>