public static void substract (ISession session) throws PersistenceException {
IDataQuery dq = session.createDataQuery();
dq.setTarget(session.getObjectDef("doc"));
//IBinaryFunction用来计算加、减、乘、除
IBinaryFunction bf = (IBinaryFunction) session.createBinaryFunction(SATConstants.BIN_SUBSTRACT, "f1"); //计算方式为减,计算后的值放在f1字段中
bf.setFirstArg(dq.getFieldDef("hs_modified_time"));
bf.setSecondArg(dq.getFieldDef("hs_created_time"));
dq.addField(bf);
//IHaving用来对运算后的字段做过滤
IHaving having = dq.getHaving();
IFieldFilter ff = dq.createFieldFilter();
ff.setFieldDef(bf);
ff.setValue(new Long(3600));
ff.setOperator(SATConstants.SQL_OP_GREATER); //过滤条件为计算后的值f1大于3600
having.addFilter(ff);
//需要对做计算的字段GroupBy
IGroupBy gb1 = dq.createGroupBy();
gb1.setGroupByField(dq.getFieldDef("hs_created_time"));
dq.addGroupBy(gb1);
IGroupBy gb2 = dq.createGroupBy();
gb2.setGroupByField(dq.getFieldDef("hs_modified_time"));
dq.addGroupBy(gb2);
System.out.println("**********************sql*****************************" + dq.getSQL(new ArgList(0), session, new StringBuffer("a"))); //查看sql是否正确
IBusObject bo = dq.queryOne(session);
if (bo != null) {
System.out.println("*******************value********************************" + bo.getField("f1")); //查询一个数据看看数值(有可能为null)
}
}
建议在流程结束时计算出来并存放到域中,这样可以减小数据库查询时的压力