2014-11-22 46 views
0

嗨,我是openbravo初学者。我想知道从HQL查询结果集返回的对象。通常我可以返回列表或字符串。当我试图返回对象它的显示错误就像不能将对象转换为字符串。如何在Openbravo中使用HQL查询结果集返回对象

这里我的目标是: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"); 
     } 
} 

在上面statment我有例外,所以我决定做标准查询和我写的条件查询equalent上面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"); 
    } 
    } 

请任何人帮助我。我想实现上述任何一种方法。在此先感谢

+0

您还没有提供类映射 – carbontax 2014-11-23 22:04:27

+0

HQL应该明确地返回对象。你在哪一行出错? – 2014-11-25 04:57:20

回答

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