from Account ac where ac.customer.id=id得到List<Account>的对象list,list大小就是account的数量而Account.java的customer属性能获取到Customer的所有字段
统计业务员业务量SQL:select ManualManID,count(ManualManID) as totalNumber,sum(ProductQty) as totalProductQty,sum(ProductWeight) as totalProductWeight,sum(ProductVolume) as totalProductVolume,sum(ProductValue) as totalProductValue,sum(IncomeCarry) as totalIncomCarry from Oms_WayBill where Valid='True' group by ManualManID //------------------------------------ 我也要解决这问题,要用hql``StringBuffer hql=new StringBuffer(); hql.append(" select new Map( "); hql.append(" count(*) as totalNumber "); hql.append(" ,sum(productQty) as totalProductQty "); hql.append(" ,sum(productWeight) as totalProductWeight "); hql.append(" ,sum(productVolume) as totalProductVolume "); hql.append(" ,sum(productValue) as totalProductValue "); hql.append(" ,sum(incomeCarry) as totalIncomCarry "); hql.append(" ) "); hql.append(" from WayBill "); hql.append(" where valid='1' "); hql.append(" group by empl.empId "); hql.append(" order by empl.empId "); System.out.println(hql.toString()); Query query=session.createQuery(hql.toString()); List<Map> list=query.list(); for(int i=0;i<list.size();i++){ Map m=list.get(i); System.out.println("--票数:"+m.get("totalNumber") +"--总件数:"+m.get("totalProductQty") +"--总重量:"+m.get("totalProductWeight") +"--总体积:"+m.get("totalProductVolume") +"--总价值:"+m.get("totalProductValue") +"--总收入:"+m.get("totalIncomCarry") ); }//运行结果: --票数:2--总件数:20--总重量:200--总体积:2000--总价值:1000--总收入:800 --票数:1--总件数:10--总重量:100--总体积:1000--总价值:500--总收入:400现在还差个业务员,高手来解决啊··
StringBuffer ehql=new StringBuffer(); ehql.append("select distinct empl from WayBill"); Query query=session.createQuery(ehql.toString()); List<Empl> emplList=query.list(); for(Empl empl:emplList){ StringBuffer hql=new StringBuffer(); hql.append(" select new Map( "); hql.append(" count(*) as totalNumber "); hql.append(" ,sum(productQty) as totalProductQty "); hql.append(" ,sum(productWeight) as totalProductWeight "); hql.append(" ,sum(productVolume) as totalProductVolume "); hql.append(" ,sum(productValue) as totalProductValue "); hql.append(" ,sum(incomeCarry) as totalIncomCarry "); hql.append(" ) "); hql.append(" from WayBill "); hql.append(" where valid='1' "); hql.append(" and empl=? "); System.out.println(hql.toString()); query=session.createQuery(hql.toString()); query.setEntity(0, empl); List<Map> list=query.list(); for(int i=0;i<list.size();i++){ Map m=list.get(i); System.out.println(empl.getEmpName() +"--票数:"+m.get("totalNumber") +"--总件数:"+m.get("totalProductQty") +"--总重量:"+m.get("totalProductWeight") +"--总体积:"+m.get("totalProductVolume") +"--总价值:"+m.get("totalProductValue") +"--总收入:"+m.get("totalIncomCarry") ); } } tx.commit(); }//运行结果:张三--票数:2--总件数:20--总重量:200--总体积:2000--总价值:1000--总收入:800 李四--票数:1--总件数:10--总重量:100--总体积:1000--总价值:500--总收入:400
Unable to locate appropriate constructor on class[.....] 是在这个类里没有找到对应的构造函数吧? 你在这个类里加个成员变量,用来接COUNT(customer.accounts.id))的值,这个类后加的成员变量与表没有对应映射没有关系,只要这个类与表有对应关系,你后加多少成员变量都没有关系,然后把这个成员变量的赋值加到构造函数里,new Customer(customer.id,customer.name,........,COUNT(customer.accounts.id))是一定要有对应相同参数个数的构造函数的!!!
问题解决 最终发现HQL要这样写 "SELECT new Customer(customer.id,customer.name,........,CAST(SIZE(customer.accounts) AS int)) FROM Customer customer GROUP BY customer.id,customer.name......."这其中,size是Hibernate里的函数,GROUP BY 不能写成 GROUP BY customer,只能写成 GROUP BY cusromer.id,customer.name..... 哈哈,终于完成了两个难题,以前还以为HQL(查询的字段)中不能使用括号嵌套,现在发现原来可以嵌套经验,大家分享一下,这样以后就可以通过查主表直接得到它对应的子记录数量了
1.在Customer.hbm.xml中 配置 set 的 lazy="false"
2.根据Customer的ID或者其它 查询Customer
3.想要什么 拿什么
4.把分给我!
你用new Customer 肯定不行 因为Customer 对象肯定没有COUNT(customer.accounts.id) 这个对应的字段
要么你重新建个VO试试
要么你用SQL 查询后自己封装PO
我也要解决这问题,要用hql``StringBuffer hql=new StringBuffer();
hql.append(" select new Map( ");
hql.append(" count(*) as totalNumber ");
hql.append(" ,sum(productQty) as totalProductQty ");
hql.append(" ,sum(productWeight) as totalProductWeight ");
hql.append(" ,sum(productVolume) as totalProductVolume ");
hql.append(" ,sum(productValue) as totalProductValue ");
hql.append(" ,sum(incomeCarry) as totalIncomCarry ");
hql.append(" ) ");
hql.append(" from WayBill ");
hql.append(" where valid='1' ");
hql.append(" group by empl.empId ");
hql.append(" order by empl.empId ");
System.out.println(hql.toString());
Query query=session.createQuery(hql.toString());
List<Map> list=query.list();
for(int i=0;i<list.size();i++){
Map m=list.get(i);
System.out.println("--票数:"+m.get("totalNumber")
+"--总件数:"+m.get("totalProductQty")
+"--总重量:"+m.get("totalProductWeight")
+"--总体积:"+m.get("totalProductVolume")
+"--总价值:"+m.get("totalProductValue")
+"--总收入:"+m.get("totalIncomCarry")
);
}//运行结果:
--票数:2--总件数:20--总重量:200--总体积:2000--总价值:1000--总收入:800
--票数:1--总件数:10--总重量:100--总体积:1000--总价值:500--总收入:400现在还差个业务员,高手来解决啊··
//暂时先用HQL嵌套解决
public void wayBillStatics() {
Session session = HibernateSessionFactoryUtil.getSessionFactory()
.getCurrentSession();
Transaction tx = session.beginTransaction();
StringBuffer ehql=new StringBuffer();
ehql.append("select distinct empl from WayBill");
Query query=session.createQuery(ehql.toString());
List<Empl> emplList=query.list();
for(Empl empl:emplList){
StringBuffer hql=new StringBuffer();
hql.append(" select new Map( ");
hql.append(" count(*) as totalNumber ");
hql.append(" ,sum(productQty) as totalProductQty ");
hql.append(" ,sum(productWeight) as totalProductWeight ");
hql.append(" ,sum(productVolume) as totalProductVolume ");
hql.append(" ,sum(productValue) as totalProductValue ");
hql.append(" ,sum(incomeCarry) as totalIncomCarry ");
hql.append(" ) ");
hql.append(" from WayBill ");
hql.append(" where valid='1' ");
hql.append(" and empl=? ");
System.out.println(hql.toString());
query=session.createQuery(hql.toString());
query.setEntity(0, empl);
List<Map> list=query.list();
for(int i=0;i<list.size();i++){
Map m=list.get(i);
System.out.println(empl.getEmpName()
+"--票数:"+m.get("totalNumber")
+"--总件数:"+m.get("totalProductQty")
+"--总重量:"+m.get("totalProductWeight")
+"--总体积:"+m.get("totalProductVolume")
+"--总价值:"+m.get("totalProductValue")
+"--总收入:"+m.get("totalIncomCarry")
);
}
}
tx.commit();
}//运行结果:张三--票数:2--总件数:20--总重量:200--总体积:2000--总价值:1000--总收入:800
李四--票数:1--总件数:10--总重量:100--总体积:1000--总价值:500--总收入:400
是在这个类里没有找到对应的构造函数吧?
你在这个类里加个成员变量,用来接COUNT(customer.accounts.id))的值,这个类后加的成员变量与表没有对应映射没有关系,只要这个类与表有对应关系,你后加多少成员变量都没有关系,然后把这个成员变量的赋值加到构造函数里,new Customer(customer.id,customer.name,........,COUNT(customer.accounts.id))是一定要有对应相同参数个数的构造函数的!!!
用lazy="false"那项目还不卡死
我是要在列表页面通过是Customer 下是否有 Account 来判断是否显示编辑按钮,
如果我查一百条Customer,而每个Customer 下都有一百条Account 那项目也得卡死了
最终发现HQL要这样写
"SELECT new Customer(customer.id,customer.name,........,CAST(SIZE(customer.accounts) AS int)) FROM Customer customer GROUP BY customer.id,customer.name......."这其中,size是Hibernate里的函数,GROUP BY 不能写成 GROUP BY customer,只能写成 GROUP BY cusromer.id,customer.name.....
哈哈,终于完成了两个难题,以前还以为HQL(查询的字段)中不能使用括号嵌套,现在发现原来可以嵌套经验,大家分享一下,这样以后就可以通过查主表直接得到它对应的子记录数量了
或者query.setResultTransformer(Transformers.aliasToBean(xx.class));