您的位置:首页 > Web前端

去除left join fetch中的多余记录

2006-10-14 09:45 399 查看
Class 和Student 存在一对多关系,如果class表中id为1的记录有1条,student表中对应class id为1的记录有2条,如下执行hql

select class from Class as class left join fetch Class.Student where class.Id=1

我们希望得到的是 一条class记录,其中包含2条student子记录,但是hql返回的list中却是有2条这样的记录

改变hql

select distinc class from Class as class left join fetch Class.Student where class.Id=1

返回的结果还是那样,这里只是在生成sql的时候加上了distinct,

我们可以通过将结果集放到一个set中,利用set的性质来去除多余记录

Set classSet = new LinkedHashSet(list);//这里list是hql返回的结果

list.clear();

list.addAll(classSet);

return list;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: