hibernate双向一对多,在 多 方查询,n+1问题,高手帮帮忙

三千大千世界 2015-01-18 02:13:08
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(好像是这个名字)做了这个加载的事情),求大神告诉一下解决方案,万分感谢啊
...全文
85 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧