Windchill8.0中使用高级查询:根据Name查询

背景介绍: 在Windchill中根据Name模糊查询出最后版序的WTProduct 分析:在用到Name为条件的查询时,在Windchill 8.0中是在WTProductMaster表的Name字段上建立了UPPER函数索引,所以从性能考虑,应该用 带函数的高级查询 解决: 查询示例代码如下: /** * 获得WTProductMaster类的最后版序的WTProduct实例 * @param ob * @return * @throws WTException */ public WTProduct getLatestVersion(String productMasterName) throws WTException{ QuerySpec qs = new QuerySpec(); //添加查询类 int productIndex = qs.appendClassList(WTProduct.class, true); int masterIndex = qs.appendClassList(WTProductMaster.class, false); //获得查询WTProduct的最后版序的查询条件 SearchCondition expression = VersionControlHelper.getSearchCondition(WTProduct.class, true); qs.appendWhere(expression, new int[]{productIndex}); //将WTProduct和WTProductMaster关联 qs.appendAnd(); expression = new SearchCondition(WTProduct.class, "masterReference.key.id", WTProductMaster.class, WTAttributeNameIfc.ID_NAME); qs.appendWhere(expression, new int[]{productIndex, masterIndex}); //将用到SQL函数在MasterName上进行查询,应为数据库表中对Name字段建立了UPPER函数索引 qs.appendAnd(); ClassAttribute upperName = new ClassAttribute(WTProductMaster.class, WTProductMaster.NAME); SQLFunction upperNameFunction = SQLFunction.newSQLFunction(SQLFunction.UPPER, upperName); ColumnExpression cExpression = ConstantExpression.newExpression (productMasterName.toUpperCase()); expression = new SearchCondition(upperNameFunction, SearchCondition.EQUAL, cExpression); qs.appendWhere(expression, new int[]{masterIndex}); System.out.println(qs); QueryResult qr = PersistenceServerHelper.manager.query(qs); WTProduct product = null; while(qr.hasMoreElements()){ //result.length为1 Object[] result = (Object[])qr.nextElement(); product = (WTProduct)result[0]; } return product; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章