无法通过排序进行内连接

时间:2012-11-23 13:41:28

标签: hibernate

我有以下域名模型:

class Department {
    Contact primaryContact
    Company company
}

当我执行JPQL查询时

from Department e 
left join e.primaryContact 
where (e.company.id=?) order by e.name asc

我得到以下SQL:

select *aliases* 
     left outer join contact contact1_ 
     on department0_.contact_id=contact1_.ID 
where 
      department0_.company_id=? 
order by department0_.name desc

但是当我试图执行

from Department e 
left join e.primaryContact 
where (e.insuranceCompany.id=?) 
order by e.primaryContact.name asc

我明白了:

select *aliases* 
  from department department0_ 
       left outer join contact contact1_ on 
       department0_.primary_contact_id = contact1_.ID 
       cross join contact contact2_ 
 where 
         department0_.primary_contact_id = contact2_.ID 
    and department0_.company_id = ? order by contact2_.name desc

区别在于

cross join contact contact2_ where department0_.primary_contact_id=contact2_.ID

因此,在按primaryContact.name

排序时,我总是有内连接

在这种情况下如何执行左连接? (我正在使用Hibernate 3.6.10)

提前谢谢。

1 个答案:

答案 0 :(得分:0)

链式表达式总是会导致内部联接。为左连接实体分配别名,并使用别名:

from Department e 
left join e.primaryContact contact
where (e.insuranceCompany.id=?) 
order by contact.name asc