坑的地方,2个默认不一样,仔细想想符合实际,记法:OL,ME
OneToMany.class:
FetchType fetch() default FetchType.LAZY; 因为多方数据量大,先find 一 get多 用懒加载 根据实际应用场景,完全有必要懒加载
ManyToOne.class
FetchType fetch() default FetchType.EAGER; 因为 多 查一,只关联一条主表数据,直接 用立即加载,根据实际应用场景,无所谓
一查多 (观察sql日志的from 后是否join
LAZY:
One的get方法时 执行sql获取 多的集合 from 多方数据表 where 外键=
EAGER
findOne一句sql获取全部 from cst_customer customer0_ left outer join
多查一
One:默认LAZY
MANY EAGER:一条sql, join
MANY LAZY: 两条sql 1sql.查M表,带外键字段 2sql.用外键查 主表
One:EAGER
MANY EAGER: get前2条(1.join 已经查了全部数据 ,2多表又去查了自己——感觉多余)
MANY LAZY: 2条sql,1多表查自己,2join查主表
视频案例 https://www.bilibili.com/video/av53910024?p=85
多查一时,双 FetchType.EAGER 多表多余sql
Hibernate: select linkman0_.lkm_id as lkm_id1_1_0_,
linkman0_.lkm_cust_id as lkm_cust9_1_0_,
linkman0_.lkm_email as lkm_emai2_1_0_,
linkman0_.lkm_gender as lkm_gend3_1_0_,
linkman0_.lkm_memo as lkm_memo4_1_0_,
linkman0_.lkm_mobile as lkm_mobi5_1_0_,
linkman0_.lkm_name as lkm_name6_1_0_,
linkman0_.lkm_phone as lkm_phon7_1_0_,
linkman0_.lkm_position as lkm_posi8_1_0_,
customer1_.cust_id as cust_id1_0_1_,
customer1_.cust_address as cust_add2_0_1_,
customer1_.cust_industry as cust_ind3_0_1_,
customer1_.cust_level as cust_lev4_0_1_,
customer1_.cust_name as cust_nam5_0_1_,
customer1_.cust_phone as cust_pho6_0_1_,
customer1_.cust_source as cust_sou7_0_1_ from cst_linkman linkman0_ left outer join cst_customer customer1_ on linkman0_.lkm_cust_id=customer1_.cust_id where linkman0_.lkm_id=?
Hibernate: select linkmans0_.lkm_cust_id as lkm_cust9_1_0_,
linkmans0_.lkm_id as lkm_id1_1_0_,
linkmans0_.lkm_id as lkm_id1_1_1_,
linkmans0_.lkm_cust_id as lkm_cust9_1_1_,
linkmans0_.lkm_email as lkm_emai2_1_1_,
linkmans0_.lkm_gender as lkm_gend3_1_1_,
linkmans0_.lkm_memo as lkm_memo4_1_1_,
linkmans0_.lkm_mobile as lkm_mobi5_1_1_,
linkmans0_.lkm_name as lkm_name6_1_1_,
linkmans0_.lkm_phone as lkm_phon7_1_1_,
linkmans0_.lkm_position as lkm_posi8_1_1_ from cst_linkman linkmans0_ where linkmans0_.lkm_cust_id=?