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; }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章