【问题标题】:How to Return Object using HQL query resultset in Openbravo如何在 Openbravo 中使用 HQL 查询结果集返回对象
【发布时间】:2015-01-20 21:07:35
【问题描述】:

您好,我是 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");
    }
  }

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

【问题讨论】:

  • 你没有提供类映射
  • HQL 应该肯定会返回对象。您在哪一行出现错误?

标签: object return hql hibernate-criteria openbravo


【解决方案1】:

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

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");
    }

【讨论】:

    【解决方案2】:

    您在哪一行出现错误?

    试试这个

     //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");
    

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2016-11-16
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多