Hibernate Query等效的sql

时间:2013-11-22 06:59:27

标签: java hibernate join hql

有人可以帮我写这个查询的等效hql

SELECT IFNULL(oea.`organization_name`,'') FROM `consultation_appointment` ca 
JOIN `organization_employee_association` oea ON ca.`consultation_id` = oea.id
JOIN professional_profile pp ON oea.`employee_profile_id` = pp.profile_id

我能够像这样加入第一个JOIN

select ca.name from ConsultationAppointment ca join ca.consultation oea

由于ConsultationAppointment类具有organization_employee_association变量,因此更容易加入,因此难以将organization_employee_association直接映射到organization_employee_association类。

即使是GORM标准查询也很有帮助。

1 个答案:

答案 0 :(得分:1)

HQL不允许加入两个无关联的实体,您应该使用笛卡尔积。

String query = "SELECT ca.name FROM consultation_appointment ca JOIN organization_employee_association oea, professional_profile pp WHERE oea.employee_profile_id = pp.profile_id";
List<String> caNames = session.createQuery(query).list();

另一种可能性是使用方法createSQLQuery()。它为执行任意连接提供了更大的灵活性。

String query = "SELECT ca.name FROM consultation_appointment ca JOIN organization_employee_association oea ON ca.consultation_id = oea.id JOIN professional_profile pp ON oea.employee_profile_id = pp.profile_id";
List<String> caNames = session.createSQLQuery(query).list();