title: Spring data jpa specification for unidirectional many-to-one
body: <pre><code>public class Hotel{
      @ManyToOne(optional = false)
      @NotNull
      @JoinColumn(name = "hotel_city", referencedColumnName = "city_id")
      private City city;
  }
  
  public class City{
      @Column(name = "public_id", updatable = false, unique = true)
      @NotEmpty
      private String publicId;
  }
  </code></pre>
  
  <p>I'd like to find all hotels which city's publicId equals to given string.</p>
  
  <pre><code>public Predicate toPredicate(Root&lt;Hotel&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
                  Subquery&lt;City&gt; subquery = query.subquery(City.class);
                  Root&lt;City&gt; subRoot = subquery.from(City.class);
                  Predicate cityIdPredicate = cb.equal(subRoot.get("publicId"), criteria.getValue().toString());
  
                  return cb.equal(root.get("city"), cityIdPredicate);
  }
  </code></pre>
  
  <p>I'm trying to do using code above to </p>
  
  <ol>
  <li>select city in city table which publicId is equals to given string;</li>
  <li>get hotel's city and compare it with the result of #1.</li>
  </ol>
  
  <p>It seems something goes wrong. It's my first time using specification, any tips are helpful.
  Thanks.</p>