ArrayList<CustomerPayment> cp = null;
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery("select c.type, c.date, sum(c.amount) from CustomerPayment c where c.date like '%" + year + "' and c.type='Cash'");
cp = (ArrayList<CustomerPayment>)q.list();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(cp.get(0).getAmount()+"");
我的函数中有上面的代码。然而; System.out.println(cp.get(0).getAmount()+"");
我无法像这样在数组列表中获取我的元素。
如何获取对象金额中的金额?
最佳答案
我想您应该在查询中按某个字段进行group by
,因为您使用SUM
。
然后,您可以在带有 3 个参数的 CustomerPayment
中创建构造函数,并使用您的参数select new CustomerPayment(...)
。
关于java - 我怎样才能到达 arraylist 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16328086/