文章目录
Windchill高级查询之多表查询
前言
实际开发场景中,需要查询所有带有BOM结构的部件,并且部件的阶段(IBA属性)为指定值。了解上篇”根据IBA属性查询部件“的同学,不难想到子查询可以解决部件阶段为指定值,那么存在BOM结构怎么过滤?本章主要讲述Windchill之间的多表连接查询ClassTableExpression类的使用。
在此之前我们需先了解TableExpression和ClassTableExpression。
一、TableExpression
- 定义:TableExpression是一个抽象类,代表的是数据库表的表达。这个类是构建SQL查询的基础组件之一。
- 用途:
- 提供一个公共的接口,供具体的表表达式(如ClassTableExpression)进行扩展。
- 用于支持查询的生成和条件的添加。
- 特点:作为一个抽象类,
TableExpression
不直接实现任何具体的表或视图功能,而是为子类提供了一些公共的方法和属性。
二、ClassTableExpression
-
定义:ClassTableExpression是TableExpression 的一个具体实现,专门用于表示与 Java 类相对应的数据库表。每个业务对象通常都有一个对应的ClassTableExpression。
-
用途:
- 允许开发者在构建查询时以面向对象的方式处理数据库表。
- 提供对特定类的属性的引用,使得查询可以直接使用 Java 对象的属性名,而不是数据库字段名。
-
特点:
- 包含了与类映射相关的信息,例如类名、表名、字段名等。
- 支持创建和管理表的别名,方便进行复杂查询。
-
总结:
TableExpression是一个抽象的概念,提供了构建查询的基础,而ClassTableExpression是其具体实现,专门用于表示与Java类相关的数据库表。
三、QuerySpec的方法appendWhere
public void appendWhere(WhereExpression a_whereExpression,
TableExpression[] a_tableExpressions,
String[] a_aliases)throws QueryException
-
含义:增加where子句
-
参数:
- a_whereExpression-一个查询条件
- a_tableExpressions -一个数组,数组里面的值是from后面的表(类)
- a_aliases-一个数组,数组里面的值是类的位置
-
代码
public class TestAppendWhere3 { /** * @param args * @throws WTException * @throws WTPropertyVetoException */ public static void main(String[] args) throws WTPropertyVetoException, WTException { QueryResult qr=getQr(); } public static String CLASSNAME=TestClass.class.getName(); public static QueryResult getQr() throws WTException, WTPropertyVetoException{ if (!RemoteMethodServer.ServerFlag) { String method = "getQr"; try { return (QueryResult) RemoteMethodServer.getDefault().invoke (method, CLASSNAME, null, new Class[] { }, new Object[] { }); } catch (Exception exp) { exp.printStackTrace(); } } QuerySpec select = new QuerySpec(); int partIndex = select.appendClassList(wt.part.WTPartMaster.class, true); QuerySpec subSelect = new QuerySpec(); subSelect.getFromClause().setAliasPrefix("B"); int altIndex = subSelect.appendClassList(wt.part.WTPartAlternateLink.class, false); subSelect.appendSelect(new ClassAttribute( wt.part.WTPartAlternateLink.class, "thePersistInfo.theObjectIdentifier.id"), new int[] { altIndex }, true); TableExpression[] tables = new TableExpression[2]; String[] aliases = new String[2]; tables