Reputation: 93
Hi I am openbravo beginner. I would like to know about the returning object from HQL query resultset. Normally I could return list or string. When I am trying to return Object its showing error like cannot cast object to string.
Here my object is : 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");
}
}
In the above statment I got exception so i decided to do criteria query and i wrote criteria query equalent to above HQL query but unfortunatly if condition second part is returning 0 as result. But I dont know why I am getting different result in same kind of query. The above HQL query is properly entering into if condition. but the problem is casting.
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");
}
}
Please any one help me. I would like to implement any one of above method. Thanks in advance
Upvotes: 0
Views: 716
Reputation: 93
Thanks a lot for your answer. Finally I got solution by own
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");
}
Upvotes: 0
Reputation: 1356
At what line are you getting error?
try this
//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");
Upvotes: 0