hibernate双向一对多,在 多 方查询,n+1问题,高手帮帮忙
CategoryDetail类
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private long categoryDetailId;
@Column
private String categoryDetailName;
@Column
private String enabledFlag;
@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="categoryId")
private Category category;
Category类
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private long categoryId;
@Column
private String categoryName;
@Column
private long userId;
@Column
private String enabledFlag;
@OneToMany
@Fetch(FetchMode.JOIN)
@JoinColumn(name="categoryId")
private List<CategoryDetail> details = new ArrayList<CategoryDetail>();
用的spring集成hibernate,都是4.X的版本,查询Category的时候,只有一条sql,是Category join CategoryDetail的
但是查询CategoryDetail的时候,就有两条sql,
select
categoryde0_.categoryDetailId as category1_1_1_,
categoryde0_.categoryId as category4_1_1_,
categoryde0_.categoryDetailName as category2_1_1_,
categoryde0_.enabledFlag as enabledF3_1_1_,
category1_.categoryId as category1_0_0_,
category1_.categoryName as category2_0_0_,
category1_.enabledFlag as enabledF3_0_0_,
category1_.userId as userId4_0_0_
from
CategoryDetail categoryde0_
left outer join
Category category1_
on categoryde0_.categoryId=category1_.categoryId
where
categoryde0_.categoryDetailId=?
第二条:
select
details0_.categoryId as category4_0_1_,
details0_.categoryDetailId as category1_1_1_,
details0_.categoryDetailId as category1_1_0_,
details0_.categoryId as category4_1_0_,
details0_.categoryDetailName as category2_1_0_,
details0_.enabledFlag as enabledF3_1_0_
from
CategoryDetail details0_
where
details0_.categoryId=?
加lazy是不起作用的,
为什么CategoryDetail(多方)join拿到Category(一方)之后,进行resolve的时候,Category还要再去查询一遍CategoryDetail表啊(源码里面有个doinitializedEntity(好像是这个名字)做了这个加载的事情),求大神告诉一下解决方案,万分感谢啊