【问题标题】:why hibernate did't make left join on many to one为什么休眠没有使左连接多对一
【发布时间】:2015-08-07 04:45:36
【问题描述】:

我有 2 个表关系类型多对一。我想看看hibernate生成左连接

配置模型类

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "stationid")
@Fetch(FetchMode.JOIN)
Station station;

这个来自休眠代码的示例 sql

 select
    tollopn0_.dt as dt7_0_,
    tollopn0_.shift as shift7_0_,
    tollopn0_.stationid as stationid7_0_,
    tollopn0_.createTime as createTime7_0_,
    tollopn0_.creator as creator7_0_,
    tollopn0_.remarsk as remarsk7_0_,
    tollopn0_.updateTime as updateTime7_0_,
    tollopn0_.updator as updator7_0_,
    tollopn0_.line as line7_0_,
    tollopn0_.rev_cash as rev10_7_0_,
    tollopn0_.rev_etc as rev11_7_0_,
    tollopn0_.sold_bail as sold12_7_0_,
    tollopn0_.sold_changeEasyPass as sold13_7_0_,
    tollopn0_.sold_changeSmartCard as sold14_7_0_,
    tollopn0_.sold_fee as sold15_7_0_,
    tollopn0_.sold_qty as sold16_7_0_,
    tollopn0_.sold_reserv as sold17_7_0_,
    tollopn0_.trf_etc as trf19_7_0_,
    tollopn0_.trf_trf1 as trf20_7_0_,
    tollopn0_.trf_trf2 as trf21_7_0_,
    tollopn0_.trf_trf3 as trf22_7_0_,
    tollopn0_.trf_trf4 as trf23_7_0_,
    tollopn0_.uap_causeOther as uap24_7_0_,
    tollopn0_.uap_causeOverPAD as uap25_7_0_,
    tollopn0_.uap_causeRemarsk as uap26_7_0_,
    tollopn0_.uap_causeSeeError as uap27_7_0_,
    tollopn0_.uap_nocCard as uap28_7_0_,
    tollopn0_.uap_nocMotorcycle as uap29_7_0_,
    tollopn0_.uap_nocOther as uap30_7_0_,
    tollopn0_.uap_nocOutOfCard as uap31_7_0_,
    tollopn0_.uap_nocRemarsk as uap32_7_0_,
    tollopn0_.uap_nocSoildier as uap33_7_0_,
    tollopn0_.uap_t05Ambulance as uap34_7_0_,
    tollopn0_.uap_t05FireTruck as uap35_7_0_,
    tollopn0_.uap_t05King as uap36_7_0_,
    tollopn0_.uap_t05LuangRoyal as uap37_7_0_,
    tollopn0_.uap_t05Mailcoaches as uap38_7_0_,
    tollopn0_.uap_t05Military as uap39_7_0_,
    tollopn0_.uap_t05PoliceCar as uap40_7_0_,
    tollopn0_.uap_t05PoliceMotorcycle as uap41_7_0_,
    tollopn0_.uap_t50Other as uap42_7_0_,
    tollopn0_.uap_uapT50Remarsk as uap43_7_0_,
    tollopn0_.uap_uap10Bath as uap44_7_0_,
    tollopn0_.uap_uap20Bath as uap45_7_0_,
    tollopn0_.uap_uap5Bath as uap46_7_0_,
    tollopn0_.uap_uapCanKept10Bath as uap47_7_0_,
    tollopn0_.uap_uapCanKept20Bath as uap48_7_0_,
    tollopn0_.uap_uapCanKept5Bath as uap49_7_0_,
    tollopn0_.uap_uapLoyal as uap50_7_0_,
    tollopn0_.uap_uapMotorcycle as uap51_7_0_,
    tollopn0_.uap_uapOther as uap52_7_0_,
    tollopn0_.uap_uapPolice as uap53_7_0_,
    tollopn0_.uap_uapRemarsk as uap54_7_0_,
    tollopn0_.uap_uapSoildier as uap55_7_0_,
    tollopn0_.cpn_sold_c60 as cpn66_7_0_,
    tollopn0_.cpn_sold_c75 as cpn67_7_0_,
    tollopn0_.cpn_used_c30 as cpn68_7_0_,
    tollopn0_.cpn_used_c40 as cpn69_7_0_,
    tollopn0_.cpn_used_c42 as cpn70_7_0_,
    tollopn0_.cpn_used_c45 as cpn71_7_0_,
    tollopn0_.cpn_used_c50 as cpn72_7_0_,
    tollopn0_.cpn_used_c53 as cpn73_7_0_,
    tollopn0_.cpn_used_c55 as cpn74_7_0_,
    tollopn0_.cpn_used_c60 as cpn75_7_0_,
    tollopn0_.cpn_used_c64 as cpn76_7_0_,
    tollopn0_.cpn_used_c70 as cpn77_7_0_,
    tollopn0_.cpn_used_c75 as cpn78_7_0_,
    tollopn0_.rev_cpn as rev79_7_0_ 
from
    trf_trx_toll tollopn0_ 
where
    tollopn0_.dt=? 
    and tollopn0_.shift=? 
    and tollopn0_.stationid=? 
    and tollopn0_.linetype='OPN'

11:37:56,588 TRACE BasicBinder:82 - 绑定参数 [1] 作为 [TIMESTAMP] - Thu Aug 06 00:00:00 ICT 2015 11:37:56,589 TRACE BasicBinder:82 - 绑定参数 [2] 作为 [INTEGER] - 1 11:37:56,589 TRACE BasicBinder:82 - 绑定参数 [3] 作为 [VARCHAR] - 111

Hibernate: 
select
    station0_.id as id5_0_,
    station0_.lineid as lineid5_0_,
    station0_.name as name5_0_,
    station0_.revetc as revetc5_0_ 
from
    trf_mst_station station0_ 
where
    station0_.id=?

11:37:56,570 TRACE BasicBinder:82 - 绑定参数 [1] 为 [VARCHAR] - 111

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    你说关联必须懒加载,使用

    fetch = FetchType.LAZY
    

    所以 Hibernate 懒惰地获取关联。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      相关资源
      最近更新 更多