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

Object 如何在Openbravo中使用HQL查询结果集返回对象,object,return,hql,hibernate-criteria,openbravo,Object,Return,Hql,Hibernate Criteria,Openbravo,嗨,我是openbravo初学者。我想知道HQL查询结果集返回的对象。通常我可以返回列表或字符串。当我试图返回对象时,会出现显示错误,如无法将对象转换为字符串 这里我的目标是:ShipmentInOut private ShipmentInOut getShipment(String documentNo) { String query = "select id from MaterialMgmtShipmentInOut where documentNo='" + documentNo

嗨,我是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");
        }
}
private ShipmentInOut getshipping(字符串文档编号){
String query=“从MaterialMgmtShipmentInOut中选择id,其中documentNo=”+documentNo
+“'and salesTransaction='Y'”;
查询结果集=OBDal.getInstance().getSession().createQuery(查询);
List shpmntCritList=resultset.List();
if(shpmntCritList!=null&&shpmntCritList.size()>0){
返回shpmntCritList.get(0);
}否则{
抛出新的OBException(“装运”+文件编号+“未找到”);
}
}
在上面的陈述中,我遇到了异常,所以我决定执行条件查询,并编写了与上述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");
    }
  }
private ShipmentInOut getshipping(字符串文档编号){
log.info()
OBCriteria shpmntCrit=OBDal.getInstance().createCriteria(ShipmentInOut.class);
shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_DOCUMENTNO,DOCUMENTNO));
shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY\u SALESTRANSACTION,true));
List shpmntCritList=shpmntCrit.List();
if(shpmntCritList!=null&&shpmntCritList.size()>0){
返回shpmntCritList.get(0);
}否则{
抛出新的OBException(“装运”+文件编号+“未找到”);
}
}

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

您在哪一行收到错误

试试这个

 //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");
//log.info()
OBCriteria shpmntCrit=OBDal.getInstance().createCriteria(ShipmentInOut.class);
shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY_DOCUMENTNO,DOCUMENTNO));
shpmntCrit.add(Restrictions.eq(ShipmentInOut.PROPERTY\u SALESTRANSACTION,true));
如果(shpmntCrit.list().size()>0)
返回shpmntCrit.list().get(0);
其他的
抛出新的OBException(“装运”+文件编号+“未找到”);

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

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");
    }
String query=“from MaterialMgmtShipmentInOut where documentNo=”+documentNo
+“'and salesTransaction='Y'”;
查询结果集=OBDal.getInstance().getSession().createQuery(查询);
List resultlist=newarraylist(resultset.List());
如果(resultset.list().size()>0){
返回resultlist.get(0);
}否则{
抛出新的OBException(“装运”+文件编号+“未找到”);
}

您尚未提供mappingsHQL类,它应该明确地将对象返回给您。你在哪一行出错?