将sql查询转换为jpql(JPA)

时间:2014-12-11 03:50:13

标签: java mysql hibernate jpa

我有一个名为LeadGeneration和Lead的JPA注释模型类。我需要使用JPQL从上面的两个类中获取结果集。我已经完成了一个与我想要的结果集相当的SQL。请告诉我如何将此SQL转换为JPQL。必须将generated_employee_id和status作为参数。

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() );

2 个答案:

答案 0 :(得分:0)

确保lead_generation有铅的集合。每个对象“。”变量必须是对象的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();

答案 1 :(得分: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);