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

📄 loader.java

📁 这是一份人力资源管理系统的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		// if we know there is exactly 1 row, we can skip.		// it would be great if we could _always_ skip this;		// it is a problem for <key-many-to-one>		if ( isSingleRowLoader() && id!=null ) {			resultId = id;		}		else {			Type idType = persister.getIdentifierType();			resultId = (Serializable) idType.nullSafeGet(rs, suffixedKeyColumns[i], session, null); //problematic for <key-many-to-one>!			if ( id!=null && resultId!=null && id.equals(resultId) ) resultId = id; //use the id passed in		}				return resultId==null ?			null :			new Key(resultId, persister);	}	/**	 * Check the version of the object in the <tt>ResultSet</tt> against	 * the object version in the session cache, throwing an exception	 * if the version numbers are different	 */	private void checkVersion(		final int i, 		final Loadable persister, 		final Serializable id, 		final Object version, 		final ResultSet rs, 		final SessionImplementor session)	throws HibernateException, SQLException {		if (version!=null) { //null version means the object is in the process of being loaded somewhere else in the ResultSet			Type versionType = persister.getVersionType();			Object currentVersion = versionType.nullSafeGet(rs, suffixedVersionColumNames[i], session, null);			if ( !versionType.equals(version, currentVersion) ) {				throw new StaleObjectStateException( persister.getMappedClass(), id );			}		}	}	/**	 * Resolve any ids for currently loaded objects, duplications within the 	 * <tt>ResultSet</tt>, etc. Instantiate empty objects to be initialized from the  	 * <tt>ResultSet</tt>. Return an array of objects (a row of results) and an  	 * array of booleans (by side-effect) that determine whether the corresponding	 * object should be initialized.	 */	private Object[] getRow(		final ResultSet rs,		final Loadable[] persisters,		final String[] suffixes,		final Key[] keys,		final Object optionalObject,		final Key optionalObjectKey,		final LockMode[] lockModes,		final List hydratedObjects,		final SessionImplementor session) 	throws HibernateException, SQLException {		int cols = persisters.length;		if ( log.isDebugEnabled() ) log.debug( "result row: " + StringHelper.toString(keys) );		Object[] rowResults = new Object[cols];		for ( int i=0; i<cols; i++ ) {			Object object=null;			Key key = keys[i];			if ( keys[i]==null ) {				//do nothing			}			else {				//If the object is already loaded, return the loaded one				object = session.getEntity(key);				if (object!=null) {					//its already loaded so don't need to hydrate it					instanceAlreadyLoaded(rs, i, persisters[i], suffixes[i], key, object, lockModes[i], session);				}				else {					object = instanceNotYetLoaded(						rs, i, persisters[i], suffixes[i], key, lockModes[i], optionalObjectKey, optionalObject, hydratedObjects, session					);				}			}			rowResults[i]=object;		}		return rowResults;	}	/**	 * The entity instance is already in the session cache	 */	private void instanceAlreadyLoaded(		final ResultSet rs, 		final int i, 		final Loadable persister, 		final String suffix, 		final Key key, 		final Object object, 		final LockMode lockMode, 		final SessionImplementor session)	throws HibernateException, SQLException {		if ( !persister.getMappedClass().isAssignableFrom( object.getClass() ) )			throw new WrongClassException( "loaded object was of wrong class", key.getIdentifier(), persister.getMappedClass() );		if ( LockMode.NONE!=lockMode && upgradeLocks() ) { //no point doing this if NONE was requested			if (				persister.isVersioned() &&				session.getLockMode(object).lessThan(lockMode)				// we don't need to worry about existing version being uninitialized				// because this block isn't called by a re-entrant load (re-entrant				// loads _always_ have lock mode NONE)			) {				//we only check the version when _upgrading_ lock modes				checkVersion(i, persister, key.getIdentifier(), session.getVersion(object), rs, session);				//we need to upgrade the lock mode to the mode requested				session.setLockMode(object, lockMode);			}		}	}		/**	 * The entity instance is not in the session cache	 */	private Object instanceNotYetLoaded(		final ResultSet rs, 		final int i, 		final Loadable persister, 		final String suffix, 		final Key key, 		final LockMode lockMode, 		final Key optionalObjectKey, 		final Object optionalObject, 		final List hydratedObjects, 		final SessionImplementor session)	throws HibernateException, SQLException {		Object object;		Class instanceClass = getInstanceClass(rs, i, persister, key.getIdentifier(), session);		if ( optionalObjectKey!=null && key.equals(optionalObjectKey) ) {			//its the given optional object			object=optionalObject;		}		else {			// instantiate a new instance			object = session.instantiate( instanceClass, key.getIdentifier() );		}		//need to hydrate it.		// grab its state from the ResultSet and keep it in the Session		// (but don't yet initialize the object itself)		// note that we acquire LockMode.READ even if it was not requested		LockMode acquiredLockMode = lockMode==LockMode.NONE ? LockMode.READ : lockMode;		loadFromResultSet(rs, i, object, key, suffix, acquiredLockMode, persister, session);		//materialize associations (and initialize the object) later		hydratedObjects.add(object);		return object;	}	/**	 * Hydrate the state an object from the SQL <tt>ResultSet</tt>, into	 * an array or "hydrated" values (do not resolve associations yet),	 * and pass the hydrates state to the session.	 */	private void loadFromResultSet(		final ResultSet rs, 		final int i, 		final Object object, 		final Key key, 		final String suffix, 		final LockMode lockMode, 		final Loadable rootPersister, 		final SessionImplementor session)	throws SQLException, HibernateException {		if ( log.isTraceEnabled() ) log.trace( "Initializing object from ResultSet: " + key );		// add temp entry so that the next step is circular-reference		// safe - only needed because some types don't take proper		// advantage of two-phase-load (esp. components)		session.addUninitializedEntity(key, object, lockMode);		// Get the persister for the _subclass_		Loadable persister = (Loadable) session.getPersister(object);		//This is not very nice (and quite slow):		String[][] cols = persister==rootPersister ?			suffixedPropertyColumns[i] :			getSuffixedPropertyAliases(persister, suffix);		Serializable id = key.getIdentifier();		Object[] values = hydrate(rs, id, object, persister, session, cols);		session.postHydrate(persister, id, values, object, lockMode);	}	/**	 * Determine the concrete class of an instance in the <tt>ResultSet</tt>	 */	private Class getInstanceClass(		final ResultSet rs, 		final int i, 		final Loadable persister, 		final Serializable id, 		final SessionImplementor session)	throws HibernateException, SQLException {		Class topClass = persister.getMappedClass();		if ( persister.hasSubclasses() ) {			// Code to handle subclasses of topClass			Object discriminatorValue = persister.getDiscriminatorType().nullSafeGet(				rs, suffixedDiscriminatorColumn[i], session, null			);			Class result = persister.getSubclassForDiscriminatorValue(discriminatorValue);			if (result==null) {				//woops we got an instance of another class heirarchy branch				throw new WrongClassException("Discriminator: " + discriminatorValue, id, topClass);			}			return result;		}		else {			return topClass;		}	}	/**	 * Unmarshall the fields of a persistent instance from a result set,	 * without resolving associations or collections	 */	private Object[] hydrate(		final ResultSet rs, 		final Serializable id, 		final Object object, 		final Loadable persister, 		final SessionImplementor session, 		final String[][] suffixedPropertyColumns)	throws SQLException, HibernateException {		if ( log.isTraceEnabled() ) log.trace("Hydrating entity: " + persister.getClassName() + '#' + id);		Type[] types = persister.getPropertyTypes();		Object[] values = new Object[ types.length ];		for (int i=0; i<types.length; i++) {			values[i] = types[i].hydrate(rs, suffixedPropertyColumns[i], session, object);		}		return values;	}	/**	 * Advance the cursor to the first required row of the <tt>ResultSet</tt>	 */	private void advance(		final ResultSet rs, 		final RowSelection selection, 		final SessionImplementor session) 	throws SQLException {				int firstRow = getFirstRow(selection);		if ( firstRow!=0 ) {			if ( session.getFactory().isScrollableResultSetsEnabled() ) {				// we can go straight to the first required row				rs.absolute(firstRow);			}			else {				// we need to step through the rows one row at a time (slow)				for ( int m=0; m<firstRow; m++ ) rs.next();			}		}	}	private static boolean hasMaxRows(RowSelection selection) {		return selection!=null && selection.getMaxRows()!=null;	}		private static int getFirstRow(RowSelection selection) {		if ( selection==null || selection.getFirstRow()==null ) {			return 0;		}		else {			return selection.getFirstRow().intValue();		}	}	/**	 * Should we pre-process the SQL string, adding a dialect-specific	 * LIMIT clause.	 */	private static boolean useLimit(final RowSelection selection, final Dialect dialect) {		return dialect.supportsLimit() && hasMaxRows(selection);	}		/**	 * Bind positional parameter values to the <tt>PreparedStatement</tt>	 * (these are parameters specified by a JDBC-style ?).	 */	protected int bindPositionalParameters(		final PreparedStatement st, 		final QueryParameters queryParameters, 		final int start, 		final SessionImplementor session) 	throws SQLException, HibernateException {				Object[] values = queryParameters.getPositionalParameterValues();		Type[] types = queryParameters.getPositionalParameterTypes();		int span=0;		for ( int i=0; i<values.length; i++) {			types[i].nullSafeSet( st, values[i], start + span, session );			span += types[i].getColumnSpan( session.getFactory() );		}		return span;	}	/**	 * Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.	 * Bind JDBC-style <tt>?</tt> parameters, named parameters, and

⌨️ 快捷键说明

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