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)

先感谢您。

4

1 回答 1

1

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

from Department e 
left join e.primaryContact contact
where (e.insuranceCompany.id=?) 
order by contact.name asc
于 2012-11-23T14:09:46.113 回答