如何在Entity类和CreateQuery中自我加入JPA

时间:2017-09-27 05:05:10

标签: java jpa playframework jpa-2.0 jpql

在我的程序中,我有一个搜索功能,用于管理员的姓名。 在我的数据库中,员工的经理ID等于另一员工的员工ID。现在在我的sql中我自己加入员工e到员工m

SELECT
e.EMPLOYEE_ID      AS empId,
e.FIRST_NAME       AS empFirstName,
e.LAST_NAME        AS empLastName,
m.FIRST_NAME || ' ' || m.LAST_NAME AS empMgrName
FROM
EMPLOYEES e
LEFT JOIN
EMPLOYEES m ON e.MANAGER_ID = m.EMPLOYEE_ID
WHERE
LOWER(m.FIRST_NAME || ' ' || m.LAST_NAME) LIKE '%"ManagerName"%'

如何在Play创建查询中执行此操作。我是否必须使用

创建查询
String strMgr = StringUtil.isDataNull(search.get("empMgrName"));
query.append(" FROM ");
query.append("    Employees e  ");
query.append(" WHERE 1 = 1 ");
if (!StringUtil.isNullOrEmpty(strMgr)) {
  query.append(" AND  ");
  query.append("    LOWER(m.FIRST_NAME || ' ' || m.LAST_NAME) LIKE :empMgrName ");
}
query.append(" ORDER BY ");
query.append("   EMPLOYEE_ID ");
query.append(" ASC  ");

Query listEmpQuery = JPA.em().createQuery(query.toString(), Employees.class);
if (!StringUtil.isNullOrEmpty(strMgr))
listEmpQuery.setParameter("empMgrName", "%" + strMgr.toLowerCase() + "%");
listEmpQuery.setFirstResult((page - 1) * 10);
listEmpQuery.setMaxResults(10);

List<Employees> listEmp = listEmpQuery.getResultList();

return listEmp;

以及我的员工实体

@Entity
@Table(name="EMPLOYEES")
@SequenceGenerator(name = "EMPLOYEES_SEQ", sequenceName = "EMPLOYEES_SEQ")
public class Employees {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMPLOYEES_SEQ")
public Long EMPLOYEE_ID;

public String FIRST_NAME;

public String LAST_NAME; 

public String MANAGER_ID;

0 个答案:

没有答案