一直以为,在Hiberenate中用多对一关系表达外键,并设置为延迟加载时,Hibernate不会在查主表时去查引用表。今天的测试却发现不是这么回事。
我定义了一个Topic类,其中有一个外键引用Company类:
<
many-to-one
name
="company"
class
="Company"
lazy
="no-proxy"
>
<
column
name
="COMPANY"
length
="32"
/>
</
many-to-one
>
测试HQL如下:select id, company from Topic
而我期望的SQL应当是这样:select boid, company from DT_TOPIC.
结果Hiberate Tools 生成的SQL是这样:
select
topic0_.BOID
as
col_0_0_,
topic0_.COMPANY
as
col_1_0_,
company1_.COMPID
as
COMPID409_,
company1_.COMPNAME
as
COMPNAME409_,
company1_.DESCRIPTION
as
DESCRIPT3_409_,
company1_.STATUS
as
STATUS409_
from
DT_TOPIC topic0_
inner
join
XPC_COMPANY company1_
on
topic0_.COMPANY
=
company1_.COMPID
Hibernate生成的SQL多做了两件影响性能的事情:
1, 与XPC_COMPANY进行inner join。
2,把XPC_COMPANY中的所有字段全取出来了。
这样还叫什么延迟加载啊?
哪位高手能告诉我这是为什么?