简体   繁体   中英

How can I reach my object at arraylist

  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()+"");

I have above code in function. However; System.out.println(cp.get(0).getAmount()+""); I cannot get my element on in the arraylist like that.

How can I get amount in the object's amount?

I suppose you should have group by by some field in your query because you use SUM . Then you can create constructor in CustomerPayment with 3 arguments and select new CustomerPayment(...) with your arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM