2014-12-11 163 views
-1

我有一個名爲LeadGeneration和Lead的JPA註釋模型類。我需要使用JPQL從上述兩個類中獲取結果集。我已經完成了我想要的結果集的SQL等價性。請告訴我如何將這個SQL轉換爲JPQL。必須將generated_employee_id和status作爲參數。將sql查詢轉換爲jpql(JPA)

SELECT * 
FROM lead_generation lg, lead l 
WHERE lg.lg_code = l.lead_generation AND lg.generated_employee_id = '111' AND l.status ='CONVERTED_LEAD' 
AND (first_appointment_date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW()); 

回答

0

確保lead_generation具有lead的集合。每個對象「。」變量必須是對象的setter和getter。

SELECT * FROM lead_generation lg INNER_JOIN lg.leads l WHERE lg.generated_employee_id = :id AND l.status= :status AND (lead_generation.first_appointment_date BETWEEN :min ANd :max) 

Query query = em.createQuery("SELECT * FROM lead_generation lg INNER_JOIN lg.leads l WHERE lg.generated_employee_id = :id AND l.status= :status AND (lead_generation.first_appointment_date BETWEEN :min ANd :max)"); 
query.setParameter("id", "111"); 
query.setParameter("status", "CONVERTED_LEAD"); 
query.setParameter("min", DATE_FORMAT(NOW() ,'%Y-%m-01')); 
query.setParameter("max", NOW()); 
List<lead_generation> lead_generations = (List<lead_generation>) query.getResultList(); 
0
以其他方式

,您可以使用nativeQuery,像這樣:

@Query(value="SELECT * FROM lead_generation lg, lead l WHERE lg.lg_code = l.lead_generation AND lg.generated_employee_id = ?1 AND l.status ='CONVERTED_LEAD' AND (first_appointment_date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW())",nativeQuery=true) 
List<User> findLeadGeneration(Integer id);