有fetch的查询
String sqlString =
"from Votecontext as vc join fetch vc.vote as vo where vo.voteId=? ";//
List list = getHibernateTemplate().find(sqlString, voteId);
for (int i = 0; i < list.size(); i++) {
Votecontext vc = (Votecontext) list.get(i);
System.out.println(vc.getContext());
System.out.println(vc.getVote().getTitle()); //通过Vote查找title
}
当查询语句缺少fetch时候
获取结果如下
for (int i = 0; i < list.size(); i++){
Object[] obj = (Object[]) list.get(i);
Votecontext vc = (Votecontext) obj[0];
context = vc.getVontext();
Vote vote =(Vote)obj[1];
title =vote.getTitle();
本文探讨了在Hibernate中使用fetch进行查询优化的方法。通过对比有fetch与无fetch的查询语句执行结果,展示了如何有效减少N+1查询问题,提高数据加载效率。
891

被折叠的 条评论
为什么被折叠?



