无法将值<object []>值分配给特定类型</object []>的ArrayList

时间:2014-07-18 07:33:54

标签: java arraylist

我从方法BillnAmountFetch中的数据库中获取数据,并在main方法中获取值,我想将获取的velue分配给arrayList ar

我从数据库中获取数据的代码

public List<Object[]> BillnAmountFetch(long cid) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        List<Object[]> obj = null;
        try {
            String hql = "select count(billNo), sum(total), invoiceDate from BillDetails "
                    + "where client.id=:cid "
                    + "group by invoiceDate  "
                    + "order by invoiceDate DESC";

            Query query = session.createQuery(hql);
            query.setParameter("cid", cid);
            obj = query.list();
            tx.commit();

        } catch (HibernateException e) {
            if (tx != null) {
                e.printStackTrace();
                tx.rollback();
            }
        } finally {
            session.close();
        }
        return obj;
    }

以下是我打印从上述方法

收到的数据的代码
public static void main(String[] args) {
        BillDAO bdo = new BillDAO();
        List<Object[]> lst = bdo.BillnAmountFetch(1);
        BillDetails bd = new BillDetails();
        ArrayList<BillDetails> ar = new ArrayList<BillDetails>();
        Object[] count = lst.get(0);
        Object[] amount = lst.get(1);
        Object[] invoice_dts = lst.get(2);
        System.out.println("-----------Total Bills---------- ");
        for (int x = 0; x < count.length; x++) {
            System.out.println("Total bills " + count[x]);
        }
        System.out.println("-------Total Amount--------- ");
        for (int x = 0; x < amount.length; x++) {
            System.out.println("Amount " + amount[x]);
        }
        System.out.println("----------Total Invoice date---------- ");
        for (int x = 0; x < invoice_dts.length; x++) {
            System.out.println("dates " + invoice_dts[x]);
        }
}

上述程序的输出是

-----------Total Bills---------- 
Total bills 3
Total bills 7281.00
Total bills 2014-07-15
-------Total Amount--------- 
Amount 7
Amount 14841.00
Amount 2014-07-12
----------Total Invoice date---------- 
dates 3
dates 1294.00
dates 2014-07-11

BillDetails.java

public class BillDetails implements java.io.Serializable {

   private Date invoiceDate;
   private long totalBills;
   private BigDecimal totalAmount; 
   //getter and setter
}

如何正确地为ArrayList对象ar

分配值

编辑:如果无法在ar中分配,那么我们可以将所有计数分配给一个类似于其他阵列的列表。   我必须在jsp页面中显示值。

3 个答案:

答案 0 :(得分:2)

  

我从方法BillnAmountFetch中的数据库中获取数据,并在main方法中获取值,我想将获取的velue分配给arrayList ar

你不能,你必须自己复制并适当地转换/转换元素。

请注意,BillnAmountFetch会返回List<Object[]>(对象数组列表),并且您说要将其分配给ArrayList<BillDetails>

因此有三个主要问题:

  • 允许List<Object[]>包含Object[]个实例,但不允许ArrayList<BillDetails>包含Object[]个实例;列表中的项目必须是BillDetails个实例。

  • List<Object[]>可以是任何类型的列表(LinkedListStack),它不必是ArrayList,但{{{} 1}}被声明为ar(具体而言)。

  • ArrayList似乎不太可能与BillDetails(对象的数组)分配兼容。

答案 1 :(得分:1)

除了T. J. Crowders建议,您可以将模型实体映射为Hibernate实体,如下所示:

 @Entity
 public class BillDetails implements java.io.Serializable {

   // some other related annotations

   private Date invoiceDate;
   private long totalBills;
   private BigDecimal totalAmount; 
   //getter and setter

}

然后你可以通过Hibernate自动绑定ArrayList< BillDetails >,当你这样做时:

  Query query = session.createQuery(hql , BillDetails.Class);

你需要进一步搜索它:

答案 2 :(得分:0)

我得到了解决方案。我可能无法正确表达我的问题。 我想要Scalar results。 我想要的是

 public List<BillDetails> BillnAmountFetch(long cid) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        ArrayList<BillDetails> ar = new ArrayList<BillDetails>();
        BillDetails bd = null;
        try {
            String hql = "select count(billNo), sum(total), invoiceDate from BillDetails "
                    + "where client.id=:cid "
                    + "group by invoiceDate  "
                    + "order by invoiceDate DESC";

            Query query;
            query = session.createQuery(hql);
            query.setParameter("cid", cid);

            Iterator results = query.list().iterator();
            while (results.hasNext()) {
                Object[] row = (Object[]) results.next();
                Long count = (Long) row[0];
                BigDecimal amount = (BigDecimal) row[1];
                Date dt = (Date) row[2];
                System.out.println(count + " " + amount + " " + dt);
                bd = new BillDetails();
                bd.setTotalBills(count);
                bd.setTotalAmount(amount);
                bd.setInvoiceDate(dt);
                ar.add(bd);
            }
            System.out.println("--------------------------");
            tx.commit();

        } catch (HibernateException e) {
            if (tx != null) {
                e.printStackTrace();
                tx.rollback();
            }
        } finally {
            session.close();
        }
        return ar;
    }

    public static void main(String[] args) {
        BillDAO bdo = new BillDAO();
        ArrayList<BillDetails> ar = (ArrayList<BillDetails>) bdo.BillnAmountFetch(1);
        System.out.println("In main method");
        for(BillDetails b:ar){
           System.out.println(b.getTotalBills() + " " + b.getTotalAmount() + " " + b.getInvoiceDate());
        }

    }