现在的位置: 首页 > 综合 > 正文

hibernate 连接问题

2018年05月07日 ⁄ 综合 ⁄ 共 425字 ⁄ 字号 评论关闭

1、在hibernate中,使用这样的HQL查询,默认生成如下的sql:

 select  t  from CrmBillDetail  t  where t.status>=0 and t.contract.code like '%GAD%';--hql

 select * from crm_bill_detail t1 cross join crm_contract t2 where t1.contract_id=t2.id and t1.status>=0 and t2.code like '%GDA%';

即:生成了内连接的sql,如果关联表crm_contract 中有为空的(外键为空) 那么就会查不出来,因为t1.contract_id=t2.id 这样的语句会查不出结果;

2、使用左连接解决上面的问题:

select  t  from CrmBillDetail  t  left join  t.contract  as e  where t.status>=0 and e.code like '%GAD%';--hql

【上篇】
【下篇】

抱歉!评论已关闭.