hibernate 查询 内连接还是外连接 ?

来源:互联网 发布:剧本统计软件 编辑:程序博客网 时间:2024/05/18 03:51

1)当查询中hql语句明确使用了left join(right join) 那才说明是外连接
2)如果hql语句中没有使用left join(right join) 那么默认是内连接
比如from SsDept sd where sd.store.csCode='0001'
因为store是连接点,那么此时最后生成的sql是
select ssdept0_.UID as UID7_, ssdept0_.StoreID as StoreID7_, ssdept0_.Type as Type7_, ssdept0_.dptCode as dptCode7_, ssdept0_.dptName as dptName7_, ssdept0_.dptMaster as dptMaster7_, ssdept0_.dptTel as dptTel7_, ssdept0_.dptDesc as dptDesc7_ from ssDept ssdept0_, cmStore cmstore1_ where ssdept0_.StoreID=cmstore1_.UID and cmstore1_.csCode='0001'
很明显看出是内连接吧

总结 hibernate使用连接的方式,都是自动的,所以当你使用的hql语句中直接使用关联表中的字段(非主键)时才考虑使用连接方式,假如把语句写成:
from SsDept sd where sd.store.uid=3
那么此时根本就不会使用连接,因为store.uid 是store的主键,也就是ssdept的外键,那么它就没有必要使用连接了。如果明确要使用外连接(比如左外连接)必须这么写:
select sd.dptName,s.csName from SsDept sd left join sd.store s
注意此处是不需要加on关键字的,因为配置文件中知道关联点在哪


特例) select b from stbillitem b order by b.goods.gdsCode
假如某句hql语句在order by处使用了关联表中的字段(如上)
那么结果就是它也使用了内连接的方式来查询stbillitem表
       hw 2010-12-16

原创粉丝点击