title: JPA - CriteriaQuery with "WHERE" clause
body: <p>I know this can be a very simple question to some of u, but I'm having a hard time trying to find out how to build a simple <strong>Select * From X Where X.a = :myparam</strong> using a <em>CriteriaBuilder</em>.</p>
  
  <p>Now, this is the code I have managed to build so far:</p>
  
  <pre><code>    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
      CriteriaQuery cq = cb.createQuery();
  
      Root&lt;MyClass1&gt; r = cq.from(MyClass1.class);
      cq.select(r);
  
      ParameterExpression&lt;Long&gt; p = cb.parameter(Long.class);
  
      cq.where(cb.equal(r.get("anotherClass.id"), p));
  
      javax.persistence.Query q = getEntityManager().createQuery(cq);
  </code></pre>
  
  <p>The class where I am applying this query is this one:</p>
  
  <pre><code>@Entity
  public class MyClass1 implements Serializable {
      @Id
      private Long id;
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "ANOTHERCLASS_ID")    
      private AnotherClass anotherClass;
      ...
  }
  
  @Entity
  public class AnotherClass implements Serializable {
      @Id
      private Long id;
      ...
  }
  </code></pre>
  
  <p>I just need to select all records from <strong>myclass1</strong> "WHERE" <strong>anotherClass.id = 1L</strong>, and where do I set the "1L", I know it goes in <strong>p</strong> but where?</p>
  
  <p>That's all. Looks simple, but I am really not familiar with this CriteriaBuilder thing, so hope you can have some answers.</p>
  
  <p>Thanks.</p>