如何在Openbravo中使用HQL查询结果集返回对象

时间:2014-11-22 22:05:04

标签: object return hql hibernate-criteria openbravo

嗨,我是openbravo初学者。我想知道HQL查询结果集中的返回对象。通常我可以返回列表或字符串。当我试图返回Object时,它的显示错误就像无法将对象转换为字符串一样。

我的目标是:ShipmentInOut

private ShipmentInOut getShipment(String documentNo) {
    String query = "select id from MaterialMgmtShipmentInOut where documentNo='" + documentNo
            + "' and salesTransaction='Y'";
        Query resultset = OBDal.getInstance().getSession().createQuery(query);

        List<ShipmentInOut> shpmntCritList = resultset.list();


        if (shpmntCritList != null && shpmntCritList.size() > 0) {
          return shpmntCritList.get(0);
        } else {
          throw new OBException("shipment " + documentNo + " not found");
        }
}

在上面的语句中我得到了异常,所以我决定做标准查询,并且我编写了符合上述HQL查询的条件查询,但不幸的是,如果条件第二部分返回0为 结果。但我不知道为什么我在同一种查询中获得不同的结果。上面的HQL查询正确地进入if条件。但问题是铸造。

 private ShipmentInOut getShipment(String documentNo) {

     log.info()
    OBCriteria<ShipmentInOut> shpmntCrit = OBDal.getInstance().createCriteria(ShipmentInOut.class);
    shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_DOCUMENTNO, documentNo));
    shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_SALESTRANSACTION, true));


    List<ShipmentInOut> shpmntCritList = shpmntCrit.list();
    if (shpmntCritList != null && shpmntCritList.size() > 0) {
      return shpmntCritList.get(0);
    } else {
      throw new OBException("shipment " + documentNo + " not found");
    }
  }

请任何人帮助我。我想实现上述任何一种方法。提前致谢

2 个答案:

答案 0 :(得分:0)

你在哪一方面收到错误?

试试这个

 //log.info()
 OBCriteria<ShipmentInOut> shpmntCrit =  OBDal.getInstance().createCriteria(ShipmentInOut.class);
 shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_DOCUMENTNO, documentNo));
 shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_SALESTRANSACTION, true));

 if (shpmntCrit.list().size() > 0)
   return shpmntCrit.list().get(0);
 else
   throw new OBException("shipment " + documentNo + " not found");

答案 1 :(得分:0)

非常感谢您的回答。最后我得到了自己的解决方案

String query = "from MaterialMgmtShipmentInOut where documentNo='" + documentNo
        + "' and salesTransaction='Y'";
    Query resultset = OBDal.getInstance().getSession().createQuery(query);
    List<ShipmentInOut> resultlist = new ArrayList<ShipmentInOut>(resultset.list());
    if (resultset.list().size() > 0) {
      return resultlist.get(0);
    } else {
      throw new OBException("shipment " + documentNo + " not found");
    }