2012-11-23 72 views
2

我有以下的域模型:不能使內部聯接與排序

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

我怎麼能執行左連接在這種情況下,排序時加入? (我正在使用休眠3.6.10)

在此先感謝您。

回答

0

鏈式表達式總是導致內部連接。爲左側加入的實體分配別名,並使用別名:

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