⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 normalizedentitypersister.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				int hydrateSpan = getHydrateSpan();		propertyTables = new int[hydrateSpan];		naturalOrderPropertyTables = new int[hydrateSpan];		propertyColumnNames = new String[hydrateSpan][];		propertyColumnAliases = new String[hydrateSpan][];		propertyColumnSpans = new int[hydrateSpan];		propertyFormulaTemplates = new String[hydrateSpan];				HashSet thisClassProperties = new HashSet();				Iterator iter = model.getPropertyClosureIterator();		int i=0;				while( iter.hasNext() ) {			Property prop = (Property) iter.next();			thisClassProperties.add(prop);			Table tab = prop.getValue().getTable();			String tabname = tab.getQualifiedName( getDialect(), factory.getDefaultSchema() );			propertyTables[i] = getTableId(tabname, tableNames);			naturalOrderPropertyTables[i] = getTableId(tabname, naturalOrderTableNames);			if ( prop.isFormula() ) {				propertyColumnAliases[i] = new String[] { prop.getFormula().getAlias() };				propertyColumnSpans[i] = 1;				propertyFormulaTemplates[i] = prop.getFormula().getTemplate( getDialect() );			}			else {				propertyColumnSpans[i] = prop.getColumnSpan();											String[] propCols = new String[ propertyColumnSpans[i] ];				String[] propAliases = new String[ propertyColumnSpans[i] ];				Iterator colIter = prop.getColumnIterator();				int j=0;				while ( colIter.hasNext() ) {					Column col = (Column) colIter.next();					String colname = col.getQuotedName( getDialect() );					propCols[j] = colname;					propAliases[j] = col.getAlias() + tab.getUniqueInteger() + StringHelper.UNDERSCORE;					j++;				}				propertyColumnNames[i] = propCols;				propertyColumnAliases[i] = propAliases;			}						i++;		}				//check distinctness of columns for this specific subclass only		HashSet distinctColumns = new HashSet();		checkColumnDuplication( distinctColumns, model.getKey().getColumnIterator() );		iter = model.getPropertyIterator();		while ( iter.hasNext() ) {			Property prop = (Property) iter.next();			if ( prop.isUpdateable() || prop.isInsertable() ) {				checkColumnDuplication( distinctColumns, prop.getColumnIterator() );			}			}				// subclass closure properties				ArrayList columns = new ArrayList();		ArrayList aliases = new ArrayList();		ArrayList formulaAliases = new ArrayList();		ArrayList formulaTemplates = new ArrayList();		ArrayList types = new ArrayList();		ArrayList names = new ArrayList();		ArrayList propColumns = new ArrayList();		ArrayList coltables = new ArrayList();		ArrayList formtables = new ArrayList();		ArrayList joinedFetchesList = new ArrayList();		ArrayList propTables = new ArrayList();		ArrayList definedBySubclass = new ArrayList();				iter = model.getSubclassPropertyClosureIterator();		while ( iter.hasNext() ) {			Property prop = (Property) iter.next();			names.add( prop.getName() );			definedBySubclass.add( new Boolean( !thisClassProperties.contains(prop) ) );			Table tab = prop.getValue().getTable();			String tabname = tab.getQualifiedName( getDialect(), factory.getDefaultSchema() );			Integer tabnum = new Integer( getTableId(tabname, subclassTableNameClosure) );			propTables.add(tabnum);			types.add( prop.getType() );			if ( prop.isFormula() ) {				formulaTemplates.add( prop.getFormula().getTemplate( getDialect() ) );				propColumns.add(ArrayHelper.EMPTY_STRING_ARRAY);				formulaAliases.add( prop.getFormula().getAlias() );				formtables.add(tabnum);			}			else {				Iterator colIter = prop.getColumnIterator();				String[] cols = new String[ prop.getColumnSpan() ];				int l=0;				while ( colIter.hasNext() ) {					Column col = (Column) colIter.next();					columns.add( col.getQuotedName( getDialect() ) );					coltables.add(tabnum);					cols[l++]=col.getQuotedName( getDialect() );					aliases.add( col.getAlias() + tab.getUniqueInteger() + StringHelper.UNDERSCORE );				}				propColumns.add(cols);			}			joinedFetchesList.add( new Integer(				prop.getValue().getOuterJoinFetchSetting()			) );		}		subclassColumnClosure = (String[]) columns.toArray(ArrayHelper.EMPTY_STRING_ARRAY);		subclassColumnClosureAliases = (String[]) aliases.toArray(ArrayHelper.EMPTY_STRING_ARRAY);		subclassColumnTableNumberClosure = ArrayHelper.toIntArray(coltables);		subclassPropertyTypeClosure = (Type[]) types.toArray(ArrayHelper.EMPTY_TYPE_ARRAY);		subclassPropertyNameClosure = (String[]) names.toArray(ArrayHelper.EMPTY_STRING_ARRAY);		subclassPropertyTableNumberClosure = ArrayHelper.toIntArray(propTables);		subclassFormulaAliasClosure = (String[]) formulaAliases.toArray(ArrayHelper.EMPTY_STRING_ARRAY);		subclassFormulaTemplateClosure = (String[]) formulaTemplates.toArray(ArrayHelper.EMPTY_STRING_ARRAY);		subclassFormulaTableNumberClosure = ArrayHelper.toIntArray(formtables);		subclassPropertyColumnNameClosure = (String[][]) propColumns.toArray( new String[ propColumns.size() ][] );				subclassPropertyEnableJoinedFetch = new int[ joinedFetchesList.size() ];		iter = joinedFetchesList.iterator();		int j=0;		while ( iter.hasNext() ) subclassPropertyEnableJoinedFetch[j++] = ( (Integer) iter.next() ).intValue();		propertyDefinedOnSubclass = new boolean[ definedBySubclass.size() ];		iter = definedBySubclass.iterator();		j=0;		while ( iter.hasNext() ) propertyDefinedOnSubclass[j++] = ( (Boolean) iter.next() ).booleanValue();				sqlDeleteStrings = generateDeleteStrings();		sqlInsertStrings = generateInsertStrings( false, getPropertyInsertability() );		sqlIdentityInsertStrings = isIdentifierAssignedByInsert() ? 			generateInsertStrings( true, getPropertyInsertability() ) : 			null;		sqlUpdateStrings = generateUpdateStrings( getPropertyUpdateability() );				String lockString = generateLockString();		lockers.put(LockMode.READ, lockString);		String lockExclusiveString = getDialect().supportsForUpdate() ? 			lockString + " for update" : 			lockString;		lockers.put(LockMode.UPGRADE, lockExclusiveString);		String lockExclusiveNowaitString = getDialect().supportsForUpdateNowait() ? 			lockString + " for update nowait" : 			lockExclusiveString;		lockers.put(LockMode.UPGRADE_NOWAIT, lockExclusiveNowaitString);		sqlVersionSelectString = generateSelectVersionString();		sqlConcreteSelectString = generateConcreteSelectString();				Class mappedClass = model.getMappedClass();				// SUBCLASSES				int subclassSpan = model.getSubclassSpan() + 1;		subclassClosure = new Class[subclassSpan];		subclassClosure[subclassSpan-1] = mappedClass;		if ( model.isPolymorphic() ) {			subclassesByDiscriminatorValue.put(discriminatorValue, mappedClass);			discriminatorValues = new String[subclassSpan];			discriminatorValues[subclassSpan-1] = discriminatorSQLString;			tableNumbers = new int[subclassSpan];			int id = getTableId(				model.getTable().getQualifiedName( getDialect(), factory.getDefaultSchema() ),				subclassTableNameClosure			);			tableNumbers[subclassSpan-1] = id;			notNullColumns = new String[subclassSpan];			notNullColumns[subclassSpan-1] =  subclassTableKeyColumns[id][0]; //( (Column) model.getTable().getPrimaryKey().getColumnIterator().next() ).getName();		}		else {			discriminatorValues = null;			tableNumbers = null;			notNullColumns = null;		}				iter = model.getSubclassIterator();		int k=0;		while ( iter.hasNext() ) {			Subclass sc = (Subclass) iter.next();			subclassClosure[k] = sc.getMappedClass();			try {				if ( model.isPolymorphic() ) {					Object disc = new Integer(k+1);					subclassesByDiscriminatorValue.put( disc, sc.getMappedClass() );					discriminatorValues[k] = disc.toString();					int id = getTableId(						sc.getTable().getQualifiedName( getDialect(), factory.getDefaultSchema() ),						subclassTableNameClosure					);					tableNumbers[k] = id;					notNullColumns[k] = subclassTableKeyColumns[id][0]; //( (Column) sc.getTable().getPrimaryKey().getColumnIterator().next() ).getName();				}			}			catch (Exception e) {				throw new MappingException("Error parsing discriminator value", e );			}			k++;		}				propertyHasColumns = new boolean[sqlUpdateStrings.length];		for ( int m=0; m<sqlUpdateStrings.length; m++ ) {			propertyHasColumns[m] = sqlUpdateStrings[m]!=null;		}				initSubclassPropertyAliasesMap(model);			}		private static final void reverse(Object[] objects, int len) {		Object[] temp = new Object[len];		for (int i=0; i<len; i++) {			temp[i] = objects[len-i-1];		}		for (int i=0; i<len; i++) {			objects[i] = temp[i];		}	}	private static final String[] reverse(String[] objects) {		int len = objects.length;		String[] temp = new String[len];		for (int i=0; i<len; i++) {			temp[i] = objects[len-i-1];		}		return temp;	}	private static final String[][] reverse(String[][] objects) {		int len = objects.length;		String[][] temp = new String[len][];		for (int i=0; i<len; i++) {			temp[i] = objects[len-i-1];		}		return temp;	}		protected int getPropertyTableNumber(String propertyName) {		String[] propertyNames = getPropertyNames();		for ( int i=0; i<propertyNames.length; i++ ) {			if ( propertyName.equals( propertyNames[i] ) ) return propertyTables[i];		}		return 0;	}		protected void handlePath(String path, Type type) {		if ( 			type.isAssociationType() &&			( (AssociationType) type).usePrimaryKeyAsForeignKey()		) {			tableNumberByPropertyPath.put( path, new Integer(0) );		}		else {			String propertyName = StringHelper.root(path);			tableNumberByPropertyPath.put( path, new Integer( getPropertyTableNumber(propertyName) ) );		}	}			public String fromTableFragment(String alias) {		return subclassTableNameClosure[0] + ' ' + alias;	}		public String getTableName() {		return subclassTableNameClosure[0];	}		private JoinFragment outerjoin(String name, boolean innerJoin, boolean includeSubclasses) {		JoinFragment outerjoin = factory.getDialect().createOuterJoinFragment();		for ( int i=1; i<subclassTableNameClosure.length; i++ ) {			if (includeSubclasses || isClassOrSuperclassTable[i]) {				outerjoin.addJoin(					subclassTableNameClosure[i],					alias(name, i),					StringHelper.qualify( name, getIdentifierColumnNames() ),					subclassTableKeyColumns[i],					innerJoin && isClassOrSuperclassTable[i] ? 						JoinFragment.INNER_JOIN : 						JoinFragment.LEFT_OUTER_JOIN				);			}		}		return outerjoin;	}		private static int getTableId(String tableName, String[] tables) {		for ( int tab=0; tab<tables.length; tab++ ) {			if ( tableName.equals( tables[tab] ) ) return tab;		}		throw new AssertionFailure("table not found");	}		public String[] toColumns(String alias, String property) throws QueryException {				if ( ENTITY_CLASS.equals(property) ) {			// This doesn't actually seem to work but it *might*			// work on some dbs. Also it doesn't work if there			// are multiple columns of results because it			// is not accounting for the suffix:			// return new String[] { getDiscriminatorColumnName() };						return new String[] { discriminatorFragment(alias).toFragmentString() };		}				int tab = ( (Integer) tableNumberByPropertyPath.get(property) ).intValue();				return super.toColumns( alias(alias, tab), property );	}		public String[] toColumns(String alias, int i) {		int tab = subclassPropertyTableNumberClosure[i];		return StringHelper.qualify( 			alias(alias, tab), 			subclassPropertyColumnNameClosure[i]		);	}		private String concretePropertySelectFragment(String alias, boolean[] includeProperty) {		int propertyCount = getPropertyNames().length;		SelectFragment frag = new SelectFragment();		for ( int i=0; i<propertyCount; i++ ) {			if ( includeProperty[i] ) { //ie. updateable, not a formula				frag.addColumns( 					alias( alias, propertyTables[i] ), 					propertyColumnNames[i], 					propertyColumnAliases[i] 				);				//don't need to handle formulas 'cos they aren't updateable!			}		}		return frag.toFragmentString();	}		public String propertySelectFragment(String alias, String suffix) {				SelectFragment frag = new SelectFragment()			.setSuffix(suffix);		for ( int i=0; i<subclassColumnClosure.length; i++ ) {			String subalias = alias( alias, subclassColumnTableNumberClosure[i] );			frag.addColumn( subalias, subclassColumnClosure[i], subclassColumnClosureAliases[i] );		}		for ( int i=0; i<subclassFormulaTemplateClosure.length; i++ ) {			String subalias = alias( alias, subclassFormulaTableNumberClosure[i] );			frag.addFormula( subalias, subclassFormulaTemplateClosure[i], subclassFormulaAliasClosure[i] );		}		if ( hasSubclasses() ) {			return ", " + 				discriminatorFragment(alias)					.setReturnColumnName( getDiscriminatorAlias(), suffix )					.toFragmentString() + 				frag.toFragmentString();		}		else {			return frag.toFragmentString();		}	}	private CaseFragment discriminatorFragment(String alias) {		CaseFragment cases = getDialect().createCaseFragment();				for ( int i=0; i< discriminatorValues.length; i++ ) {			cases.addWhenColumnNotNull(				alias( alias, tableNumbers[i] ),				notNullColumns[i],				discriminatorValues[i]			);		}				return cases;	}		private static String alias(String name, int tableNumber) {		if (tableNumber==0) return name;		return name + StringHelper.UNDERSCORE + tableNumber + StringHelper.UNDERSCORE;	}		public String fromJoinFragment(String alias, boolean innerJoin, boolean includeSubclasses) {		return outerjoin(alias, innerJoin, includeSubclasses).toFromFragmentString();	}	public String whereJoinFragment(String alias, boolean innerJoin, boolean includeSubclasses) {		return outerjoin(alias, innerJoin, includeSubclasses).toWhereFragmentString();	}	public String queryWhereFragment(String alias, boolean innerJoin, boolean includeSubclasses) throws MappingException {		String result = whereJoinFragment(alias, innerJoin, includeSubclasses);		String rootAlias = alias( alias, naturalOrderTableNames.length-1 ); //urgh, ugly!		if ( hasWhere() ) result += " and " + getSQLWhereString(rootAlias);		return result;	}		public String[] getIdentifierColumnNames() {		return tableKeyColumns[0];	}	protected String[] getActualPropertyColumnNames(int i) {		return propertyColumnNames[i];	}		protected String getFormulaTemplate(int i) {		return propertyFormulaTemplates[i];	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -