如何使用coalesce
或case statement
在 JPA 2 中使用 CriteriaBuilder
对于许多记录,initiatedBy 将为空,因此这些记录的employeeName 将为空。对于那些在数据库表中 InitialBy 员工为空的项目,我想显示System Generated if为空。employeeName
我在实体中有以下关系
项目
@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;
员工
@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<Project> employeeProject;
@OneToMany(mappedBy = "initiatedBy")
private Set<Project> employeeInitiatedBy;
.....
在DAOImpl课上我有
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Project> c = cb.createQuery(Project.class);
Root<Project> emp = c.from(Project.class);
c.orderBy(cb.desc(emp.get("projectNo")));
c.select(emp);
TypedQuery<Project> q = entityManager.createQuery(c);
我试过了
Coalesce<String> coalesce = cb.coalesce();
coalesce.value(emp.<String>get("employeeName"));
coalesce.value("System Generated");
但是,当我跑步时,我遇到了异常
java.lang.IllegalArgumentException: Unable to resolve attribute
[employeeName] against path
对此的任何帮助都是非常可观的。