hibernate 小记

来源:互联网 发布:电脑测试网络运行 编辑:程序博客网 时间:2024/05/19 17:02

当使用 jion fetch 时,查询出多条重复记录,这是因为多表连续查询的结果

可以加关键词DISTINCT查出唯一值(不过用这个性能会慢些)

例子

 "select cl from CheckList cl" +" left join fetch cl.checkCell cc" +" left join fetch cl.assets a" +" left join fetch a.currentCycle c"+" left join fetch c.location l"+" left join fetch a.origin o"+" left join fetch a.app app"+" left join fetch c.cycleState cs"+" left join fetch c.department dp"+" left join fetch dp.mainNode dpm"+" left join fetch c.assetsState ats"+" left join fetch a.expands ed"+" left join fetch ed.value vl"+" left join fetch vl.field fd"+" left join fetch a.contract cont"+" left join fetch a.parts p"+" left join fetch p.expands ped"+" left join fetch ped.value pvl"+" left join fetch pvl.field pfd"+" where cc.id in (:ids)";
这里多表链接,如果其它表有多条记录对于,查出的列表就可能包含多条重复的记录
 
    "select DISTINCT cl from CheckList cl"      +" left join fetch cl.checkCell cc"      +" left join fetch cl.assets a"      +" left join fetch a.currentCycle c"     +" left join fetch c.location l"     +" left join fetch a.origin o"     +" left join fetch a.app app"     +" left join fetch c.cycleState cs"     +" left join fetch c.department dp"     +" left join fetch dp.mainNode dpm"     +" left join fetch c.assetsState ats"     +" left join fetch a.expands ed"     +" left join fetch ed.value vl"     +" left join fetch vl.field fd"     +" left join fetch a.contract cont"     +" left join fetch a.parts p"     +" left join fetch p.expands ped"     +" left join fetch ped.value pvl"     +" left join fetch pvl.field pfd"     +" where cc.id in (:ids)";

这个加了DISTINCT ,就不会重复了



HQL查询的聚集函数

avg: 计算属性平均值

count:数量

max:统计对象最大值

min:统计对象最小值

sum:统计属性值综合

例:

select count(*) from Userselect max(u.age) from User as u




0 0
原创粉丝点击