结论: 使用迫切加载时是级联查询,只执行一条sql, 使用懒加载是执行多条sql. 和之前的认知不同
执行一条sql的前提:查询条件是id,且级联对象迫切加载
实体对象
@Id
private Long id;
private String uid;
private Integer trade_type;
private Integer status;
@JsonFormat(pattern= "yyyy-MM-dd HH:mm:ss")
private LocalDateTime deliver_time_start;
private Long user_id;
@ApiModelProperty("收货(配送)方式\n" +
"0 配送\n" +
"1 自提")
private Integer receipt_type;
@ApiModelProperty("应付款")
@JsonSerialize(using = PriceToStringSerializer.class)
private Integer pay_price;
@ApiModelProperty("已付款")
@JsonSerialize(using = PriceToStringSerializer.class)
private Integer paid_price;
@ApiModelProperty("退款金额")
@JsonSerialize(using = PriceToStringSerializer.class)
private Integer refund;
private Integer refunds_dest;
private Integer pay_type;
@ApiModelProperty("真实的已配送商品金额,扣除订单完成前的退款金额")
@JsonSerialize(using = PriceToStringSerializer.class)
private Integer real_total_sale_price;
@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name = "child_order_id")
private List orderItemDtos;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@JoinColumn(name = "location_id")
private LocationDto locationDto;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "driver_id")
@JsonUnwrapped
private Employee employee;
@JsonIgnore
private Long deliver_range_fee_id;
@ApiModelProperty("订单分拣编号")
private Integer order_serial_num;
private Boolean delivered;
@Transient
@ApiModelProperty("配送线路")
private String line;
@Transient
@ApiModelProperty("配送线路Id")
private Integer lineId;
@ApiModelProperty("配送状态:0,待分拣;1,待配送;2,已完成")
public Integer getDeliveryStatus(){
if(status<2)
return 0;
if(delivered == true)
return 2;
return 1;
}
@ApiModelProperty("差额,正数代表需补款,负数代表需退款")
@JsonSerialize(using = PriceToStringSerializer.class)
public Integer getDetla(){
return pay_price-paid_price;
}
执行的sql语句
Hibernate:
select
childorder0_.id as id1_0_0_,
childorder0_.deliver_range_fee_id as deliver_2_0_0_,
childorder0_.deliver_time_start as deliver_3_0_0_,
childorder0_.delivered as delivere4_0_0_,
childorder0_.driver_id as driver_17_0_0_,
childorder0_.location_id as locatio18_0_0_,
childorder0_.order_serial_num as order_se5_0_0_,
childorder0_.paid_price as paid_pri6_0_0_,
childorder0_.pay_price as pay_pric7_0_0_,
childorder0_.pay_type as pay_type8_0_0_,
childorder0_.real_total_sale_price as real_tot9_0_0_,
childorder0_.receipt_type as receipt10_0_0_,
childorder0_.refund as refund11_0_0_,
childorder0_.refunds_dest as refunds12_0_0_,
childorder0_.status as status13_0_0_,
childorder0_.trade_type as trade_t14_0_0_,
childorder0_.uid as uid15_0_0_,
childorder0_.user_id as user_id16_0_0_,
employee1_.id as id1_17_1_,
employee1_.deleted as deleted3_17_1_,
employee1_.position_id as position4_17_1_,
employee1_.status as status5_17_1_,
employee1_.true_name as true_nam2_17_1_,
employee1_.user_id as user_id6_17_1_,
employee1_.warehouse_id as warehous7_17_1_,
orderitemd2_.child_order_id as child_or3_18_2_,
orderitemd2_.id as id1_18_2_,
orderitemd2_.id as id1_18_3_,
orderitemd2_.bar_code as bar_code2_18_3_,
orderitemd2_.child_order_id as child_or3_18_3_,
orderitemd2_.count as count4_18_3_,
orderitemd2_.item_id as item_id5_18_3_,
orderitemd2_.item_uid as item_uid6_18_3_,
orderitemd2_.procurement_item_id as procurem7_18_3_,
orderitemd2_.real_count as real_cou8_18_3_,
orderitemd2_.real_price as real_pri9_18_3_,
orderitemd2_.real_weight as real_we10_18_3_,
orderitemd2_.rejected_weight as rejecte11_18_3_,
orderitemd2_.sale_price as sale_pr12_18_3_,
orderitemd2_.item_specification_id as item_sp13_18_3_
from
child_order childorder0_
left outer join
manager_employee employee1_
on childorder0_.driver_id=employee1_.id
left outer join
order_item orderitemd2_
on childorder0_.id=orderitemd2_.child_order_id
where
childorder0_.id=?
Hibernate:
select
locationdt0_.id as id1_13_0_,
locationdt0_.address as address2_13_0_,
locationdt0_.conn_person as conn_per3_13_0_,
locationdt0_.conn_phone as conn_pho5_13_0_,
locationdt0_.name as name4_13_0_
from
location locationdt0_
where
locationdt0_.id=?
如果把location的关联改成迫切加载, 则执行的sql为.
Hibernate:
select
childorder0_.id as id1_0_0_,
childorder0_.deliver_range_fee_id as deliver_2_0_0_,
childorder0_.deliver_time_start as deliver_3_0_0_,
childorder0_.delivered as delivere4_0_0_,
childorder0_.driver_id as driver_17_0_0_,
childorder0_.location_id as locatio18_0_0_,
childorder0_.order_serial_num as order_se5_0_0_,
childorder0_.paid_price as paid_pri6_0_0_,
childorder0_.pay_price as pay_pric7_0_0_,
childorder0_.pay_type as pay_type8_0_0_,
childorder0_.real_total_sale_price as real_tot9_0_0_,
childorder0_.receipt_type as receipt10_0_0_,
childorder0_.refund as refund11_0_0_,
childorder0_.refunds_dest as refunds12_0_0_,
childorder0_.status as status13_0_0_,
childorder0_.trade_type as trade_t14_0_0_,
childorder0_.uid as uid15_0_0_,
childorder0_.user_id as user_id16_0_0_,
employee1_.id as id1_17_1_,
employee1_.deleted as deleted3_17_1_,
employee1_.position_id as position4_17_1_,
employee1_.status as status5_17_1_,
employee1_.true_name as true_nam2_17_1_,
employee1_.user_id as user_id6_17_1_,
employee1_.warehouse_id as warehous7_17_1_,
locationdt2_.id as id1_13_2_,
locationdt2_.address as address2_13_2_,
locationdt2_.conn_person as conn_per3_13_2_,
locationdt2_.conn_phone as conn_pho5_13_2_,
locationdt2_.name as name4_13_2_,
orderitemd3_.child_order_id as child_or3_18_3_,
orderitemd3_.id as id1_18_3_,
orderitemd3_.id as id1_18_4_,
orderitemd3_.bar_code as bar_code2_18_4_,
orderitemd3_.child_order_id as child_or3_18_4_,
orderitemd3_.count as count4_18_4_,
orderitemd3_.item_id as item_id5_18_4_,
orderitemd3_.item_uid as item_uid6_18_4_,
orderitemd3_.procurement_item_id as procurem7_18_4_,
orderitemd3_.real_count as real_cou8_18_4_,
orderitemd3_.real_price as real_pri9_18_4_,
orderitemd3_.real_weight as real_we10_18_4_,
orderitemd3_.rejected_weight as rejecte11_18_4_,
orderitemd3_.sale_price as sale_pr12_18_4_,
orderitemd3_.item_specification_id as item_sp13_18_4_
from
child_order childorder0_
left outer join
manager_employee employee1_
on childorder0_.driver_id=employee1_.id
left outer join
location locationdt2_
on childorder0_.location_id=locationdt2_.id
left outer join
order_item orderitemd3_
on childorder0_.id=orderitemd3_.child_order_id
where
childorder0_.id=?