title: EntityManager query by joinColumn
body: <p>I have a <code>Login</code> entity and a <code>Customer</code> entity. <code>Login.username</code> is a foreign key in the customer table. Hence the following line in the Java <code>Customer</code> POJO</p>
  
  <pre><code>@OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "username", nullable = false)
  private Login login;
  </code></pre>
  
  <p>My question is this: Is there a simple way to query the <code>customer</code> table using <code>username</code>? Or must I first fetch <code>login</code> by <code>username</code> and then <code>customer</code> by <code>login</code>?</p>
  
  <p>Here is the JPA criteria query. And, yes, I would prefer to use criteria query.</p>
  
  <pre><code>public Customer getCustomerByUsername(String username) throws EntityNotFoundException {
      CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;Customer&gt; criteriaQuery = criteriaBuilder.createQuery(Customer.class);
      Root&lt;Customer&gt; root = criteriaQuery.from(Customer.class);
      Path&lt;String&gt; path = root.&lt;String&gt;get("username");
      criteriaQuery.where(criteriaBuilder.equal(path, username));
      return entityManager.createQuery(criteriaQuery).getSingleResult();
  }
  </code></pre>
  
  <p>The line <code>Path&lt;String&gt; path = root.&lt;String&gt;get("username")</code> is throwing an exception saying that <code>username ... is not present.</code></p>