title: Spring Specification Criteria Multiple Joins ? How?
body: <p>I got stuck using a Spring Project with Spring Data + specification + criteria api.</p>
  
  <p>I will try to simulate the situation with general entities we used write to get easy example.</p>
  
  <p><strong>The Entities:</strong> 
  Consider all attributes of the each entity is passed on the constructor showed below</p>
  
  <p><strong>Country</strong>(Long id, String name, String iso)<br>
  <strong>State</strong>(Long id, String name, String iso)<br>
  <strong>City</strong>(Long id, String name, String iso)<br>
  <br></p>
  
  <p>This is my repository:</p>
  
  <pre><code>public interface CityRepository extends PagingAndSortingRepository&lt;City, Integer&gt;, JpaSpecificationExecutor&lt;City&gt; {
  
  }
  </code></pre>
  
  <p>As you can see, I don't need to implement anything on the repository</p>
  
  <p>This is my service</p>
  
  <pre><code>@Service
  @Transactional
  public class CityService {
  
      @Autowired
      private CityRepository cityRepository;
  
      @Transactional(readOnly = true)
      public CityListVO findByNameLike(String name, PageRequest pageRequest) {
          name = "%" + name + "%";
  
          if (pageRequest == null) {
              List&lt;City&gt; result = cityRepository.findAll(fillGridCriteria(name));
              return new CityListVO(1, result.size(), result);
          } else {
              Page&lt;City&gt; result = cityRepository. findAll(fillGridCriteria(name), pageRequest);
              return new CityListVO(result.getTotalPages(), result.getTotalElements(), result.getContent());
          }
  
      }
  
      private static Specification&lt;City&gt; fillGridCriteria(String name) {
          return new Specification&lt;City&gt;() {
  
              @Override
              public Predicate toPredicate(
                      Root&lt;City&gt; root,
                      CriteriaQuery&lt;?&gt; query,
                      CriteriaBuilder builder) { 
  
               /*
                  The current return I can do a like by name, and it works fine. 
  
                  My problem is if for any reason I need to do multiple joins like the folow jpql: 
                  select ci FROM City ci, State st, Country co where ci.st = st AND st.co = co AND co.name = 'Canada';
  
                  How to do this from here ? Inside this method.
                  How is gonna be the return for this method ?
  
               */
  
                  return builder.like(root.get("name"), name.trim());
              }
  
          };
      }
  
  }
  </code></pre>