title: how could i use JPA criteria query api for joined columns?
body: <p>i am new to JPA and i have a problem with it.</p>
  
  <p>suppose that we have two tables which are related </p>
  
  <p>by a ManytoOne association, which means that</p>
  
  <p>table A stores a primary key of table B within it.</p>
  
  <p>when these two tables are mapped to JPA entities</p>
  
  <p>i have a problem for search on this situation.</p>
  
  <p>i have used an existing code from richfaces demo, to handle filtering and sorting by using</p>
  
  <p>JPA. this code is using input parameters to create criteria query.</p>
  
  <p>this is the code:</p>
  
  <pre><code>private CriteriaQuery&lt;T&gt; createSelectCriteriaQuery() {
          CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
          CriteriaQuery&lt;T&gt; criteriaQuery = criteriaBuilder.createQuery(entityClass);
          Root&lt;T&gt; root = criteriaQuery.from(entityClass);
  
          if (arrangeableState != null) {
  
              List&lt;Order&gt; orders = createOrders(criteriaBuilder, root);
              if (!orders.isEmpty()) {
                  criteriaQuery.orderBy(orders);
              }
  
              Expression&lt;Boolean&gt; filterCriteria = createFilterCriteria(criteriaBuilder, root);
              if (filterCriteria != null) {
                  criteriaQuery.where(filterCriteria);
              }
          }
  
          return criteriaQuery;
      }
      protected Expression&lt;Boolean&gt; createFilterCriteriaForField(String propertyName, Object filterValue, Root&lt;T&gt; root, CriteriaBuilder criteriaBuilder) {
          String stringFilterValue = (String) filterValue;
          if (Strings.isNullOrEmpty(stringFilterValue)) {
              return null;
          }
  
          stringFilterValue = stringFilterValue.toLowerCase(arrangeableState.getLocale());
  
          Path&lt;String&gt; expression = root.get(propertyName);
          Expression&lt;Integer&gt; locator = criteriaBuilder.locate(criteriaBuilder.lower(expression), stringFilterValue, 1);
          return criteriaBuilder.gt(locator, 0);
      }
  
  
      private Expression&lt;Boolean&gt; createFilterCriteria(CriteriaBuilder criteriaBuilder, Root&lt;T&gt; root) {
          Expression&lt;Boolean&gt; filterCriteria = null;
          List&lt;FilterField&gt; filterFields = arrangeableState.getFilterFields();
          if (filterFields != null &amp;&amp; !filterFields.isEmpty()) {
              FacesContext facesContext = FacesContext.getCurrentInstance();
  
              for (FilterField filterField : filterFields) {
                  String propertyName = (String) filterField.getFilterExpression().getValue(facesContext.getELContext());
                  Object filterValue = filterField.getFilterValue();
  
                  Expression&lt;Boolean&gt; predicate = createFilterCriteriaForField(propertyName, filterValue, root, criteriaBuilder);
  
                  if (predicate == null) {
                      continue;
                  }
  
                  if (filterCriteria == null) {
                      filterCriteria = predicate.as(Boolean.class);
                  } else {
                      filterCriteria = criteriaBuilder.and(filterCriteria, predicate.as(Boolean.class));
                  }
              }
  
          }
          return filterCriteria;
      }
  </code></pre>
  
  <p>the code is okay, when i try to filter columns(not joined columns), but when i try to</p>
  
  <p>query on joined column, the produced query is not correct and it throws exception.</p>
  
  <p>so my question is that, how could i use JPA criteria query api, to filter rows by both </p>
  
  <p>joined columns and non-joined coulmns.</p>
  
  <p>thanks</p>