2017-02-14 83 views
10

我有我想轉換到JPA標準以下的原生SQL查詢:轉換SQL左外連接查詢到JPA標準

select et.* from t_empl_tx et, t_dept d 
where et.assigned_dept = d.dept (+) 
    and et.employee_id = :employee_id 
    and (et.start_date >= d.dept_est_date and 
     et.start_date <= d.dept_close_date or 
     et.start_date is null or 
     d.dept is null) 

(注意(+),大致相當於一個左外加入這種情況。是的,我知道它表示可選表格等等)。

這裏是我的代碼嘗試:

EntityManager entityManager = getEntityManager(); 
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 
CriteriaQuery<EmployeeTransaction> criteriaQuery = criteriaBuilder.createQuery(EmployeeTransaction.class); 
Root<EmployeeTransaction> root = criteriaQuery.from(EmployeeTransaction.class); 

    // this line bombs! 
Join<EmployeeTransaction, Department> join = 
    root.join(EmployeeTransaction_.assignedDepartment).join(Department_.id).join(DepartmentCompositeId_.department, JoinType.LEFT); 

List<Predicate> predicates = new ArrayList<>(); 

predicates.add(criteriaBuilder.equal(root.get(EmployeeTransaction_.id).get(EmployeeTransactionCompositeId_.employeeId), employeeId)); 
predicates.add(criteriaBuilder.or(
    criteriaBuilder.and(
    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get(EmployeeTransaction_.requestedStartDate), join.get(Department_.id).<Date>get(DepartmentCompositeId_.departmentCreationDate)), 
    criteriaBuilder.lessThanOrEqualTo(root.<Date>get(EmployeeTransaction_.requestedStartDate), join.<Date>get(Department_.departmentCloseDate)) 
    ), 
    criteriaBuilder.isNull(root.get(EmployeeTransaction_.requestedStartDate)), 
    criteriaBuilder.isNull(join.get(Department_.id).get(DepartmentCompositeId_.departmentCreationDate)) 
)); 

criteriaQuery.select(root).where(predicates.toArray(new Predicate[]{})); 

TypedQuery<EmployeeTransaction> query = entityManager.createQuery(criteriaQuery); 
List<EmployeeTransaction> result = query.getResultList(); 

這個問題似乎是,我想加入一個字符串列,assigedDepartment,以複合ID的單個字段。這在SQL中是完全合法的,但在代碼中並不那麼容易。

一種選擇是轉換爲多個子查詢,這似乎完全消除了左外連接的點。

任何人都可以指出我做錯了什麼?

傑森

+0

沒有時間寫一個答案,但「從entity1中選擇entity1 left join entity1.field2 where ...「。如果你想讓左連接急切地獲取,你需要有「左連接獲取」。 –

+0

JoinType屬性只有三個可能的值:INNER,LEFT和RIGHT。似乎沒有左連接提取的選項。 另外,join()行有問題。即使join()返回一個SingularAttribute <>,我似乎無法像這樣鏈接命令。 – Jason

+1

在JPA Criteria查詢中,您引用了SQL查詢中不存在的表'DepartmentCompositeId'。請通過添加每個實體定義的相關部分來填寫信息 – perissf

回答

2

你應該張貼您的實體,這樣的答案可以更具體。

但是,我會試一試。

如果我是正確的,你可以重寫查詢:

select et.* 
from t_empl_tx et 
    left join t_dept d on et.assigned_dept = d.dept 
where 
    et.employee_id = :employee_id 
    and (
     et.start_date >= d.dept_est_date 
     and et.start_date <= d.dept_close_date 
     or et.start_date is null 
     or d.dept is null) 

所以,不久,你必須移動JoinType.LEFTassignedDepartment加入:

EntityManager entityManager = getEntityManager(); 
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); 
CriteriaQuery<EmployeeTransaction> criteriaQuery = criteriaBuilder.createQuery(EmployeeTransaction.class); 

Root<EmployeeTransaction> root = criteriaQuery.from(EmployeeTransaction.class); 
Join<EmployeeTransaction, Department> department = root.join(EmployeeTransaction_.assignedDepartment, JoinType.LEFT); 
Path<Date> employeeTransactionRequestedStartDate = root.get(EmployeeTransaction_.requestedStartDate); 
Path<DepartmentCompositeId> departmentId = department.get(Department_.id); 
Path<Date> departmentCreationDate = departmentId.get(DepartmentCompositeId_.departmentCreationDate) 
Path<Date> departmentCloseDate = departmentId.get(DepartmentCompositeId_.departmentCloseDate) 

criteriaQuery.select(root).where(
    criteriaBuilder.equal(root.get(EmployeeTransaction_.id).get(EmployeeTransactionCompositeId_.employeeId), employeeId), 
    criteriaBuilder.or(
     criteriaBuilder.and(
      criteriaBuilder.greaterThanOrEqualTo(employeeTransactionRequestedStartDate, departmentCreationDate)), 
      criteriaBuilder.lessThanOrEqualTo(employeeTransactionRequestedStartDate, departmentCloseDate) 
     ), 
     criteriaBuilder.isNull(employeeTransactionRequestedStartDate), 
     criteriaBuilder.isNull(departmentCreationDate) 
    ) 
); 

TypedQuery<EmployeeTransaction> query = entityManager.createQuery(criteriaQuery); 
List<EmployeeTransaction> result = query.getResultList();