title: Usage of Coalesce or Case statement in JPA
body: <p>How to use <code>coalesce</code>  or <code>case statement</code> in JPA 2 using CriteriaBuilder </p>
  
  <p>For many records initiatedBy will be empty and as a result employeeName will be null for those records. I would like to display <strong>System Generated</strong> if <code>employeeName</code> is null for those projects where initiatedBy employee is null in database table.</p>
  
  <p>I have the following relationships in Entities</p>
  
  <p><strong>Project</strong></p>
  
  <pre><code>@Entity
  @Table(name = "PROJECT") 
  public class Project {
  
  @Id
  @Column(name = "PROJECTID")
  private Long projectId;
  ....
  ....
  
  @ManyToOne
  @JoinColumn(name = "EMPLOYEENUMBER", referencedColumnName = "EMPLOYEENUMBER")
  private Employee empNumber;
  
  @ManyToOne
  @JoinColumn(name = "INITIATEDBY", referencedColumnName = "EMPLOYEENUMBER")
  private Employee initiatedBy;
  </code></pre>
  
  <p><strong>Employee</strong></p>
  
  <pre><code>@Entity
  @Table(name = "EMPLOYEES")
  public class Employee {
  
  @Id
  @Column(name = "EMPLOYEENUMBER")
  private String employeeNo;
  
  @Column(name = "EMPLOYEENAME")
  private String employeeName;
  .....
  .....
  
  @OneToMany(mappedBy = "empNumber")
  private Set&lt;Project&gt; employeeProject;
  
  @OneToMany(mappedBy = "initiatedBy")
  private Set&lt;Project&gt; employeeInitiatedBy;
  
  .....
  </code></pre>
  
  <p>in <strong>DAOImpl</strong> class I have</p>
  
  <pre><code>CriteriaBuilder cb = entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;Project&gt; c = cb.createQuery(Project.class);
  Root&lt;Project&gt; emp = c.from(Project.class);
  c.orderBy(cb.desc(emp.get("projectNo")));
  c.select(emp);
  
  TypedQuery&lt;Project&gt; q =  entityManager.createQuery(c);
  </code></pre>
  
  <p>I have tried with </p>
  
  <pre><code>Coalesce&lt;String&gt; coalesce = cb.coalesce();
  coalesce.value(emp.&lt;String&gt;get("employeeName"));
  coalesce.value("System Generated");
  </code></pre>
  
  <p>However when I run, I am getting exception as</p>
  
  <pre><code>java.lang.IllegalArgumentException: Unable to resolve attribute 
  [employeeName] against path
  </code></pre>
  
  <p>Any help on this is highly appreciable.</p>