NC6 单据查询按钮实现类:nc.md.persist.framework.imp.VOQueryPersister,可根据父类查找所有的子类

    技术2022-07-11  116

    package nc.md.persist.framework.imp; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import nc.bs.logging.Logger; import nc.bs.ml.NCLangResOnserver; import nc.jdbc.framework.mapping.MappingMeta; import nc.md.common.AssociationKind; import nc.md.data.access.NCObject; import nc.md.innerservice.MDQueryService; import nc.md.model.IAssociation; import nc.md.model.IAttribute; import nc.md.model.IBean; import nc.md.model.IBusinessEntity; import nc.md.model.ICardinality; import nc.md.model.IForeignKey; import nc.md.model.MetaDataException; import nc.md.model.MetaDataRuntimeException; import nc.md.util.MDUtil; import nc.vo.jcom.lang.StringUtil; import nc.vo.pub.CircularlyAccessibleValueObject; /** * 单据查询实现 */ final class VOQueryPersister { private IBusinessEntity relatedEntity = null; private boolean ignoreDrEqual1 = false; // 是否忽略dr为1的记录 private final static String FIRST_ORDERPATH_KEY = "@@@"; private Collection<MappingMeta> relatedMetas = null; /* tableName -> lias */ private Map<String, String> relatedTableAliasMap = new HashMap<String, String>(); private MDMultiTableDAO dao = null; /** * 以实体构造 * * @param entity */ public VOQueryPersister(IBusinessEntity entity) { initPersister(entity); } /** * 以实体构造 * * @param entity */ public VOQueryPersister(IBusinessEntity entity, boolean ignoreDrEqual1) { initPersister(entity); this.ignoreDrEqual1 = ignoreDrEqual1; } /** * 以className构造 * * @param voFullClassName */ public VOQueryPersister(String voFullClassName) { // 查找实体 IBean bean = null; try { bean = MDQueryService.lookupMDQueryService() .getBeanByFullClassName(voFullClassName); } catch (MetaDataException e) { Logger.error(NCLangResOnserver.getInstance().getStrByID("mdbusi", "voPersister-0000")/*根据VO全路径类型没有查到相应的元数据,请检查此VO有无对应的元数据模型,VO类型:*/ + voFullClassName, e); } if (bean == null) throw new MetaDataRuntimeException( NCLangResOnserver.getInstance().getStrByID("mdbusi", "voQueryPersister-0000")/*需要持久化的对象没有模型信息,或者模型信息被损坏,请检查该对象对应的模型!更详细的信息请查看日志!类名:*/ + voFullClassName); initPersister(bean); } public VOQueryPersister(String voFullClassName, boolean ignoreDrEqual1) { this(voFullClassName); this.ignoreDrEqual1 = ignoreDrEqual1; } private void initPersister(IBean bean) { relatedEntity = (IBusinessEntity) bean; if (relatedEntity == null) throw new MetaDataRuntimeException( NCLangResOnserver.getInstance().getStrByID("mdbusi", "voPersister-0001")/*需要持久化的对象没有模型信息,或者模型信息被损坏,请检查该对象对应的模型!更详细的信息请查看日志!*/); if (!MDUtil.isEntityType(relatedEntity)) throw new MetaDataRuntimeException( NCLangResOnserver.getInstance().getStrByID("mdbusi", "mdPersistUtil-0001")/*要保存的SuperVO对应的元数据不是实体类型,请检查模型!beanName:*/ + bean.getFullName()); relatedMetas = MDPersistUtil.getMappingDataOfBeanWithExtendTable( relatedEntity, null).values(); for (MappingMeta meta : relatedMetas) { relatedTableAliasMap.put(meta.getTableName(), meta.getTableName()); } dao = new MDMultiTableDAO(relatedEntity, relatedMetas, relatedTableAliasMap); } /**************************************************************** ******************************** 查询**************************** ****************************************************************/ protected NCObject queryBillImp(String billPK, boolean bLazyLoad) throws MetaDataException { // MappingData NCObject resNCObj = null; try { Object resVO = dao.retrieveByPK(billPK,ignoreDrEqual1); if (resVO == null) return null; resNCObj = NCObject.newInstance(relatedEntity, resVO); if (!bLazyLoad) queryChildrenVOSByParentObjs(new NCObject[] { resNCObj }, bLazyLoad, null, null); } catch (Exception e) { Logger.error("fail to query data", e); throw new MetaDataException( "operation failed** baseDao.retrieveByPK," + e.getMessage()); } return resNCObj; } protected Collection<NCObject> queryBillsImp(String[] billPKs, String[] subEntityName) throws MetaDataException { return queryBillsImp(billPKs, false, subEntityName); } protected Collection<NCObject> queryBillsImp(String[] pks, boolean bLazyLoad) throws MetaDataException { return queryBillsImp(pks, bLazyLoad, null); } private Collection<NCObject> queryBillsImp(String[] pks, boolean bLazyLoad, String[] subEntityName) throws MetaDataException { // MappingData Collection<NCObject> results = null; try { Collection resVOs = dao.retrieveByPKS(pks, null, ignoreDrEqual1); // final Collection resVOs = new ArrayList(); // InSqlBatchCaller caller = new InSqlBatchCaller(pks); // caller.execute(new IInSqlBatchCallBack() { // public Object doWithInSql(String inSql) // throws BusinessException, SQLException { // String primaryKey = relatedEntity.getKeyAttribute() // .getColumn().getName(); // String whereCondStr = new StringBuffer(relatedEntity // .getTable().getName() + "." + primaryKey) // .append(" in ").append(inSql).toString(); // Collection tempVos; // try { // tempVos = dao.retrieveByClouse(whereCondStr); // } catch (Exception e) { // Logger.error(e.getMessage(), e); // throw new BusinessException(e.getMessage()); // } // resVOs.addAll(tempVos); // return null; // } // }); if (resVOs == null || resVOs.size() == 0) return null; NCObject[] parentNVOs = new NCObject[resVOs.size()]; int i = 0; for (Object curvo : resVOs) { parentNVOs[i++] = NCObject.newInstance(relatedEntity, curvo); } if (!bLazyLoad) { queryChildrenVOSByParentObjs(parentNVOs, bLazyLoad, null, null, subEntityName); } results = Arrays.asList(parentNVOs); } catch (Exception e) { Logger.error("operation failed", e); throw new MetaDataException( "operation failed : baseDao.retrieveByPKS," + e.getMessage()); } return results; } protected NCObject[] queryBillsImp(String[] pks, String[] filtAttrNames, boolean bLazyLoad) throws MetaDataException { // MappingData final String[] fields = filtAttrNames;// getColumnNamesByAttrNames(filtAttrNames); NCObject[] results = null; try { Collection resVOs = dao.retrieveByPKS(pks, fields, ignoreDrEqual1); // InSqlBatchCaller caller = new InSqlBatchCaller(pks); // caller.execute(new IInSqlBatchCallBack() { // public Object doWithInSql(String inSql) // throws BusinessException, SQLException { // String primaryKey = relatedEntity.getKeyAttribute() // .getColumn().getName(); // String whereCondStr = new StringBuffer(relatedEntity // .getTable().getName() + "." + primaryKey) // .append(" in ").append(inSql).toString(); // Collection tempVos; // try { // tempVos = dao.retrieveByClouse(whereCondStr, fields); // } catch (Exception e) { // Logger.error(e.getMessage(), e); // throw new BusinessException(e.getMessage()); // } // resVOs.addAll(tempVos); // return null; // } // }); if (resVOs == null || resVOs.size() == 0) return new NCObject[pks.length]; List<NCObject> resultList = new ArrayList<NCObject>(); Map<String, NCObject> resultMap = new HashMap<String, NCObject>(); // BDObjectAdpaterFactory factory = new BDObjectAdpaterFactory(); for (Object curvo : resVOs) { NCObject ncObj = NCObject.newInstance(relatedEntity, curvo); resultMap.put((String) ncObj.getAttributeValue(relatedEntity .getKeyAttribute()), ncObj); } // 查询子表 queryChildrenVOSByParentObjs( resultMap.values().toArray(new NCObject[0]), bLazyLoad, null, null); // 按传入参数pks排序 for (String pk : pks) { resultList.add(resultMap.get(pk)); } results = resultList.toArray(new NCObject[0]); } catch (Exception e) { Logger.error("operation failed", e); throw new MetaDataException( "operation failed : MDMultiTableDAO.retrieveByClouse," + e.getMessage()); } return results; } // /** // * 根据属性名数组获取对应的字段名数组. // * @param filtAttrNames // * @return // */ // private String[] getColumnNamesByAttrNames(String[] filtAttrNames) { // String[] fields = null; // IMappingMeta mapData = MDPersistUtil.getMappingDataOfBean(relatedEntity, // null); // if (filtAttrNames == null || filtAttrNames.length == 0) // fields = mapData.getColumns(); // else { // List<String> filtCols = new ArrayList<String>(); // String[] attrs = mapData.getAttributes(); // String[] cols = mapData.getColumns(); // Set<String> filtAttrSet = new // HashSet<String>(Arrays.asList(filtAttrNames)); // //总是要包含主键属性 // filtAttrSet.add(relatedEntity.getKeyAttribute().getName()); // for (int i = 0; i < attrs.length; i++) { // if (filtAttrSet.contains(attrs[i])) // filtCols.add(cols[i]); // } // fields = filtCols.toArray(new String[0]); // } // return fields; // } /***************************** whereCondition ************************************/ protected NCObject[] queryBillsImp(String whereCondStr, String[] sunEntityPaths, String[] orderPaths) throws MetaDataException { if (sunEntityPaths == null || sunEntityPaths.length == 0) { return queryBillsImp(null, whereCondStr, true, sunEntityPaths, orderPaths); } return queryBillsImp(null, whereCondStr, false, sunEntityPaths, orderPaths); } protected NCObject[] queryBillsImp(String whereCondStr, boolean bLazyLoad, String[] orderPaths) throws MetaDataException { return queryBillsImp(null, whereCondStr, bLazyLoad, null, orderPaths); } public NCObject[] queryBillsImp(String[] tableNames, String whereCondStr, boolean bLazyLoad, String[] orderPaths) throws MetaDataException { Map<String, String> tableNameMap = new HashMap<String, String>(); for (MappingMeta meta : relatedMetas) { tableNameMap.put(meta.getTableName(), meta.getTableName()); } for (String name : tableNames) { tableNameMap.put(name, name); } return queryBillsImp(tableNameMap, whereCondStr, bLazyLoad, null, orderPaths); } protected NCObject[] queryBillsImp(String fromTableSql, String whereCondStr, boolean bLazyLoad, String[] orderPaths) throws MetaDataException { Map<String, String> tableNameMap = new HashMap<String, String>(); for (MappingMeta meta : relatedMetas) { tableNameMap.put(meta.getTableName(), meta.getTableName()); } if (!StringUtil.isEmptyWithTrim(fromTableSql)) { String[] fromTables = fromTableSql.trim().split(","); for (String tablePart : fromTables) { String[] tableParts = tablePart.trim().split("(\\s)+"); if (tableParts != null && tableParts.length > 0) { String tableName = tableParts[0]; String tableAlias = null; if (tableParts.length == 2) { tableAlias = tableParts[1]; } tableNameMap.put(tableName, tableAlias); } } } return queryBillsImp(tableNameMap, whereCondStr, bLazyLoad, null, orderPaths); } private NCObject[] queryBillsImp(Map<String, String> tableAliasMap, String whereCondStr, boolean bLazyLoad, String[] sunEntityPaths, String[] orderPaths) throws MetaDataException { if(whereCondStr == null){ whereCondStr = ""; } if (tableAliasMap == null) { tableAliasMap = relatedTableAliasMap; } Map<String, List<String>> pathMap = getOrderMap(orderPaths); NCObject[] results = null; try { if (ignoreDrEqual1) whereCondStr = whereCondStr + " and isnull(" + tableAliasMap.get(relatedEntity.getTable().getName()) + ".dr,0)=0 "; String orderPath = ""; if (pathMap.containsKey(FIRST_ORDERPATH_KEY)) { orderPath = getOrderSqlPath(pathMap.get(FIRST_ORDERPATH_KEY)); pathMap.remove(FIRST_ORDERPATH_KEY); whereCondStr += orderPath; } MDMultiTableDAO dao = new MDMultiTableDAO(relatedEntity, relatedMetas, tableAliasMap); Collection resVOs = dao.retrieveByClouse(whereCondStr); if (resVOs == null || resVOs.size() == 0) return null; results = new NCObject[resVOs.size()]; int i = 0; for (Object curvo : resVOs) { results[i++] = NCObject.newInstance(relatedEntity, curvo); } // 查询子表 queryChildrenVOSByParentObjs(results, bLazyLoad, pathMap, "", sunEntityPaths); } catch (Exception e) { Logger.error("operation failed", e); throw new MetaDataException( "operation failed : baseDao.retrieveByPK," + e.getMessage()); } return results; } protected NCObject[] queryBillsImp(String whereCondStr, String[] filtAttrNames, boolean bLazyLoad) throws MetaDataException { // MappingData // final String[] fields = getColumnNamesByAttrNames(filtAttrNames); NCObject[] results = null; try { if (ignoreDrEqual1) whereCondStr = whereCondStr + " and isnull(" + relatedTableAliasMap.get(relatedEntity.getTable() .getName()) + ".dr,0)=0 "; Collection resVOs = dao.retrieveByClouse(whereCondStr, filtAttrNames, false); if (resVOs == null || resVOs.size() == 0) return null; results = new NCObject[resVOs.size()]; int i = 0; for (Object curvo : resVOs) { results[i++] = NCObject.newInstance(relatedEntity, curvo); } // 查询子表 queryChildrenVOSByParentObjs(results, bLazyLoad, null, null); } catch (Exception e) { Logger.error("operation failed", e); throw new MetaDataException( "operation failed && baseDao.retrieveByPK," + e.getMessage()); } return results; } // /** // * 根据父亲查找所有的孩子 // * @param parentObjs // * @return 返回结果为map,子表名为key,具体子表VOS为值 // * @throws MetaDataException // */ // public Map<String, Collection> queryChildrenVOSByParentObjs(Object // parentObj) // throws MetaDataException { // Map<String, Collection> resMap = new HashMap<String, Collection>(); // NCObject parentNCObj = MDPersistUtil.getNCObject(parentObj); // if (parentObj == null) // return resMap; // IBusinessEntity parentEntity = (IBusinessEntity) // parentNCObj.getRelatedBean(); // // //所有的聚合关系 // List<IAssociation> compositeAsses = parentEntity.getAssociationsByKind( // AssociationKind.Composite, ICardinality.ASS_ALL); // if (compositeAsses != null && compositeAsses.size() > 0) { // for (IAssociation association : compositeAsses) { // IAttribute innerAttr = association.getStartAttribute(); // IBusinessEntity childEntity = (IBusinessEntity) association.getEndBean(); // String childTableName = childEntity.getTable().getName(); // if (innerAttr != null && childEntity != null) { // // //取得主表在子表中的外键名 // IForeignKey foreignKey = // childEntity.getTable().getForeignKeyWithEndTable( // parentEntity.getTable()); // if (foreignKey == null) // throw new MetaDataException("表主外键关联关系为空,起始表:" + childTableName + ",关联表:" // + parentEntity.getTable().getName()); // String parentKeyInChild = foreignKey.getStartColumn().getName(); // //组织子表查询条件 // StringBuffer whereStr = new StringBuffer("("); // CircularlyAccessibleValueObject curVO = (CircularlyAccessibleValueObject) // parentNCObj // .getModelConsistObject(); // if (curVO == null) // continue; // whereStr.append(childTableName + "." + parentKeyInChild + " = '" // + MDPersistUtil.getPrimaryKeyValue(curVO) + "'"); // if (ignoreDrEqual1) // whereStr.append(" and isnull(" + childTableName + ".dr,0)=0)"); // //暂时过滤dr=0 // // //查询子表 // Collection<MappingMeta> mapData = // MDPersistUtil.getMappingDataOfBeanWithExtendTable( // relatedEntity, parentEntity).values(); // Collection results = null; // try { // MDMultiTableDAO dao = new MDMultiTableDAO(relatedEntity); // results = // dao.retrieveByClouse(Class.forName(childEntity.getFullClassName()), // mapData, // whereStr.toString()); // } catch (Exception e) { // Logger.error("error", e); // throw new MetaDataException("operation failed!" + e.getMessage()); // } // // resMap.put(innerAttr.getName(), results); // } // } // } // return resMap; // // } private void queryChildrenVOSByParentObjs(NCObject[] parentObjs, boolean bLazyload, Map<String, List<String>> orderPathMap, String orderPath) throws MetaDataException { // IBusinessEntity parentEntity = (IBusinessEntity) // parentObjs[0].getRelatedBean(); // List<IAssociation> compositeAsses = // parentEntity.getAssociationsByKind( // AssociationKind.Composite, ICardinality.ASS_ALL); // String[] subEntityName = null; // if (compositeAsses != null && compositeAsses.size() > 0) { // subEntityName = new String[compositeAsses.size()]; // for (int i = 0; i < compositeAsses.size(); i++) { // IAttribute compAttr = compositeAsses.get(i).getStartAttribute(); // subEntityName[i] = compAttr.getName(); // } // } queryChildrenVOSByParentObjs(parentObjs, bLazyload, orderPathMap, orderPath, null); } /** * 根据父亲查找所有的孩子 * * @param parentObjs * @return * @throws MetaDataException */ private void queryChildrenVOSByParentObjs(NCObject[] parentObjs, boolean bLazyload, Map<String, List<String>> orderPathMap, String orderPath, String[] subEntityName) throws MetaDataException { if (orderPathMap == null) { orderPathMap = new HashMap<String, List<String>>(); } // FIXME cglib // queryChildrenVosWhenExtend(parentObjs,bLazyload);//remove for it if (bLazyload) return; if (parentObjs == null || parentObjs.length == 0) return; IBusinessEntity parentEntity = (IBusinessEntity) parentObjs[0] .getRelatedBean(); resolveCompositeAssocations(parentObjs, bLazyload, parentEntity, orderPathMap, orderPath, subEntityName); resolveRelationAsscocations(parentObjs, bLazyload, parentEntity, orderPathMap, orderPath); } // 目前只是支持一对一关联关系。且只查询一层关系,以免在自关联的时候,陷入死循环! // 逻辑算法和组合类似,只是有些key不同,有待以后重构... added in 2008-01-30 private void resolveRelationAsscocations(NCObject[] parentObjs, boolean bLazyload, IBusinessEntity parentEntity, Map<String, List<String>> orderPathMap, String orderPath) throws MetaDataException { List<IAssociation> relationAsses = parentEntity.getAssociationsByKind( AssociationKind.Relation, ICardinality.ASS_ALL); if (relationAsses != null && relationAsses.size() > 0) { for (IAssociation association : relationAsses) { if (association.getEndCardinality().endsWith("1"))// 1-1 resolveOne2OneRelation(association, parentObjs, parentEntity, bLazyload, orderPathMap, orderPath); else Logger.warn(NCLangResOnserver.getInstance().getStrByID("mdbusi", "voQueryPersister-0001")/*元数据持久层暂时不支持1对多关联关系的查询!*/); // resolveOne2ManyRelation(association,parentObjs,parentEntity,bLazyload); } } } private void resolveOne2OneRelation(IAssociation association, NCObject[] parentObjs, IBusinessEntity parentEntity, boolean bLazyload, Map<String, List<String>> orderPathMap, String orderPath) throws MetaDataException { IAttribute startAttr = association.getStartAttribute(); // 如果是ref类型,本身就是string存储的外键值,不需要再去查询子对象 if (MDUtil.isRefType(startAttr.getDataType())) return; // 计算属性不处理 if (startAttr.isCalculation()) return; IBusinessEntity childEntity = (IBusinessEntity) association .getEndBean(); if (startAttr != null && childEntity != null) { // 对于那些ref类型的字段,没有foreignkey // String attrNameInParent = startAttr.getName(); String pkOfChild = childEntity.getTable().getPrimaryKeyName(); // java对象上生成的供持久化使用的和外键一致的属性名 String mockFKAttrName = MDUtil.getPKFieldNameOfAttribute(startAttr); HashSet<String> pkinChildSet = new HashSet<String>(); // 组织子表查询条件 for (int i = 0; i < parentObjs.length; i++) { CircularlyAccessibleValueObject curVO = (CircularlyAccessibleValueObject) parentObjs[i] .getModelConsistObject(); if (curVO != null && curVO.getAttributeValue(mockFKAttrName) != null) { pkinChildSet.add(curVO.getAttributeValue(mockFKAttrName) .toString()); } } String whereStr = null; if (pkinChildSet.size() > 0) { whereStr = "("; whereStr += childEntity.getTable().getName() + "." + pkOfChild + " in ("; whereStr += StringUtil.getUnionStr( (String[]) pkinChildSet.toArray(new String[0]), ",", "'") + ") )"; if (ignoreDrEqual1) whereStr = whereStr + " and isnull(" + childEntity.getTable().getName() + ".dr,0)=0"; } if (orderPathMap.containsKey(orderPath)) { whereStr += getOrderSqlPath(orderPathMap.get(orderPath)); orderPathMap.remove(orderPath); } // 查询子表 Collection<MappingMeta> mapData = MDPersistUtil .getMappingDataOfBeanWithExtendTable(childEntity, parentEntity).values(); Collection results = null; try { MDMultiTableDAO dao = new MDMultiTableDAO(childEntity, mapData); results = dao.retrieveByClouse(whereStr); } catch (Exception e) { Logger.error("error", e); throw new MetaDataException("operation failed!" + e.getMessage()); } // 分发各主表的子表数据 if (results != null && results.size() > 0) { // hash化 Map<String, CircularlyAccessibleValueObject> resmap = new HashMap<String, CircularlyAccessibleValueObject>(); for (Object obj : results) { CircularlyAccessibleValueObject element = (CircularlyAccessibleValueObject) obj; resmap.put(MDPersistUtil.getPrimaryKeyValue(element), element); } // 转换为NCObject for (int i = 0; i < parentObjs.length; i++) { CircularlyAccessibleValueObject curVO = (CircularlyAccessibleValueObject) parentObjs[i] .getModelConsistObject(); if (curVO == null) continue; CircularlyAccessibleValueObject childVO = resmap.get(curVO .getAttributeValue(mockFKAttrName)); if (childVO != null) parentObjs[i].setAttributeValue(startAttr, NCObject.newInstance(childEntity, childVO)); } // 非懒加载情况,递归调用子子表 if (!bLazyload) {// FIXME 不支持,否则会进入死循环! // queryChildrenVOSByParentObjs((NCObject[])childNCObjsList.toArray(new // NCObject[0]),bLazyload); } } } } private void resolveCompositeAssocations(NCObject[] parentObjs, boolean bLazyload, IBusinessEntity parentEntity, Map<String, List<String>> orderPathMap, String curPathFromRoot, String[] subEntityNames) throws MetaDataException { // 所有的聚合关系 List<IAssociation> compositeAsses = parentEntity.getAssociationsByKind( AssociationKind.Composite, ICardinality.ASS_ALL); if (compositeAsses == null || compositeAsses.size() == 0) { return; } Set<String> subPathSet = new HashSet<String>(); boolean needSubEntityFilter = false; if (subEntityNames != null && subEntityNames.length > 0) { // 指定子实体 needSubEntityFilter = true; for (String subPath : subEntityNames) { subPathSet.add(subPath); } } for (IAssociation association : compositeAsses) { String orderPathTemp = curPathFromRoot; IAttribute compAttr = association.getStartAttribute(); final IBusinessEntity childEntity = (IBusinessEntity) association .getEndBean(); if (compAttr == null || compAttr.isCalculation() || childEntity == null) {// 计算属性 continue; } if (needSubEntityFilter && !subPathSet.contains(compAttr.getName())) {// 指定子实体过滤 continue; } String childPathFromRoot = StringUtil .isEmptyWithTrim(curPathFromRoot) ? compAttr.getName() : curPathFromRoot + "." + compAttr.getName(); String orderSqlPath = ""; if (orderPathMap.containsKey(childPathFromRoot)) { List<String> orderPathList = orderPathMap .get(childPathFromRoot); orderSqlPath = getOrderSqlPath(orderPathList); orderPathMap.remove(childPathFromRoot); } // 组织子表查询条件 List<String> pkvalues = new ArrayList<String>(); for (int i = 0; i < parentObjs.length; i++) { CircularlyAccessibleValueObject curVO = (CircularlyAccessibleValueObject) parentObjs[i] .getModelConsistObject(); if (curVO == null) continue; pkvalues.add(MDPersistUtil.getPrimaryKeyValue(curVO)); } // 查询子表 IForeignKey foreignKey = parentEntity.getTable() .getForeignKeieFromSubTable( childEntity.getTable().getName()); // 取得主表在子表中的外键名 if (foreignKey == null) throw new MetaDataException(NCLangResOnserver.getInstance().getStrByID("mdbusi", "voQueryPersister-0002")/*表主外键关联关系为空,起始表:*/ + childEntity.getTable().getName() + NCLangResOnserver.getInstance().getStrByID("mdbusi", "voQueryPersister-0003")/*,关联表:*/ + parentEntity.getTable().getName()); final String parentKeyInChild = foreignKey.getStartColumn() .getName(); final Collection<MappingMeta> mapData = MDPersistUtil .getMappingDataOfBeanWithExtendTable(childEntity, parentEntity).values(); String pkFieldName = childEntity.getTable().getName() + "." + parentKeyInChild; MDMultiTableDAO childDAO = new MDMultiTableDAO(childEntity, mapData); Collection results = childDAO.retrieveByPKS(pkFieldName, (String[]) pkvalues.toArray(new String[0]), null, ignoreDrEqual1, orderSqlPath); // 分发各主表的子表数据 Map<String, List<NCObject>> childvosMap = new HashMap<String, List<NCObject>>(); if (results != null && results.size() > 0) { // 转换为NCObject List<NCObject> childNCObjsList = new ArrayList<NCObject>(); for (Object cvoObj : results) { CircularlyAccessibleValueObject cvo = (CircularlyAccessibleValueObject) cvoObj; NCObject newObj = NCObject.newInstance(childEntity, cvo); childNCObjsList.add(newObj); } // 非懒加载情况,递归调用子子表 if (!bLazyload) queryChildrenVOSByParentObjs( (NCObject[]) childNCObjsList .toArray(new NCObject[0]), bLazyload, orderPathMap, orderPathTemp); // 分发 for (NCObject ncObj : childNCObjsList) { CircularlyAccessibleValueObject cvo = (CircularlyAccessibleValueObject) ncObj .getContainmentObject(); String parentKeyValue = (String) cvo .getAttributeValue(parentKeyInChild); List<NCObject> ncObjList = childvosMap.get(parentKeyValue); if (ncObjList == null) { ncObjList = new ArrayList<NCObject>(); childvosMap.put(parentKeyValue, ncObjList); } ncObjList.add(ncObj); } } for (int i = 0; i < parentObjs.length; i++) { CircularlyAccessibleValueObject curVO = (CircularlyAccessibleValueObject) parentObjs[i] .getModelConsistObject(); if (curVO == null) continue; Object childValues = childvosMap.get(MDPersistUtil .getPrimaryKeyValue(curVO)); if (childValues != null) { if (association.getEndCardinality().endsWith("n")) // 1-n parentObjs[i].setAttributeValue(compAttr, childValues); else { List<NCObject> ncObjList = (List<NCObject>) childValues; if (ncObjList != null && ncObjList.size() > 0) parentObjs[i].setAttributeValue(compAttr, ncObjList.get(0)); } } } } } private String getOrderSqlPath(List<String> orderPathList) { String orderSqlPath = ""; for (String orderTemp : orderPathList) { orderSqlPath += orderTemp + ","; } orderSqlPath = " order by " + orderSqlPath.substring(0, orderSqlPath.length() - 1) + " "; return orderSqlPath; } /** * @param orderPaths * a.b.c * @return (a.b->tableC.c) */ private Map<String, List<String>> getOrderMap(String[] orderPaths) { Map<String, List<String>> pathMap = new HashMap<String, List<String>>(); if (orderPaths != null && orderPaths.length > 0) { for (String orderPath : orderPaths) { if (!StringUtil.isEmptyWithTrim(orderPath)) { String tableName = relatedEntity .getAttributeByPath(orderPath).getTable().getName(); String[] paths = StringUtil.split(orderPath, "."); if (paths.length == 1) {// 只有1级路径 tableName = relatedTableAliasMap.get(tableName); } String value = paths[paths.length - 1]; String key = orderPath.substring(0, orderPath.length() - value.length()); if (StringUtil.isEmptyWithTrim(key)) {// for MainEntity // eg:name key = FIRST_ORDERPATH_KEY; } else { key = key.substring(0, key.length() - 1); } if (!pathMap.containsKey(key)) { List<String> pathList = new ArrayList<String>(); pathMap.put(key, pathList); } String salOrderVaule = tableName + "." + value; pathMap.get(key).add(salOrderVaule); } } } return pathMap; } }

    调用:

    /** * 根据where条件查询单据,返回实际的VO集合(List),请注意此处使用的where子句为正常的sql语法,不是面向模型的 * @param voClass * @param whereCondStr * @param ignoreDrEqual1 是否忽略dr为1的记录,针对NC的特殊性设置,默认为false * @param bLazyLoad 是否懒加载 * @return * @throws MetaDataException */ public Collection queryBillOfVOByCond(Class voClass, String whereCondStr, boolean ignoreDrEqual1, boolean bLazyLoad, String[] orderPaths) throws MetaDataException { NCObject[] ncObjs = new VOQueryPersister(voClass.getName(), gnoreDrEqual1).queryBillsImp( whereCondStr, bLazyLoad, orderPaths); List resList = new ArrayList(); if (ncObjs != null) { for (int i = 0; i < ncObjs.length; i++) { resList.add(ncObjs[i].getContainmentObject()); } } return resList; }
    Processed: 0.013, SQL: 9