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

📄 basicentitypersister.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	public String[] getIdentifierColumnNames() {		return rootTableKeyColumnNames;	}	protected int getIdentifierColumnSpan() {		return identifierColumnSpan;	}	protected String[] getIdentifierAliases() {		return identifierAliases;	}	public String getVersionColumnName() {		return versionColumnName;	}	protected String getVersionedTableName() {		return getTableName( 0 );	}	protected boolean[] getSubclassColumnLazyiness() {		return subclassColumnLazyClosure;	}	protected boolean[] getSubclassFormulaLazyiness() {		return subclassFormulaLazyClosure;	}	public boolean isCacheInvalidationRequired() {		return hasFormulaProperties() || ( !isVersioned() && ( entityMetamodel.isDynamicUpdate() || getTableSpan() > 1 ) );	}	public String selectFragment(String alias, String suffix) {		return identifierSelectFragment( alias, suffix ) + propertySelectFragment( alias, suffix, false );	}	public String[] getIdentifierAliases(String suffix) {		// NOTE: this assumes something about how propertySelectFragment is implemented by the subclass!		// was toUnqotedAliasStrings( getIdentiferColumnNames() ) before - now tried		// to remove that unqoting and missing aliases..		return new Alias( suffix ).toAliasStrings( getIdentifierAliases() );	}	public String[] getPropertyAliases(String suffix, int i) {		// NOTE: this assumes something about how propertySelectFragment is implemented by the subclass!		return new Alias( suffix ).toUnquotedAliasStrings( propertyColumnAliases[i] );	}	public String getDiscriminatorAlias(String suffix) {		// NOTE: this assumes something about how propertySelectFragment is implemented by the subclass!		// was toUnqotedAliasStrings( getdiscriminatorColumnName() ) before - now tried		// to remove that unqoting and missing aliases..		return entityMetamodel.hasSubclasses() ?				new Alias( suffix ).toAliasString( getDiscriminatorAlias() ) :				null;	}	public String identifierSelectFragment(String name, String suffix) {		return new SelectFragment()				.setSuffix( suffix )				.addColumns( name, getIdentifierColumnNames(), getIdentifierAliases() )				.toFragmentString()				.substring( 2 ); //strip leading ", "	}	public String propertySelectFragment(String name, String suffix, boolean allProperties) {		SelectFragment select = new SelectFragment()				.setSuffix( suffix )				.setUsedAliases( getIdentifierAliases() );		int[] columnTableNumbers = getSubclassColumnTableNumberClosure();		String[] columnAliases = getSubclassColumnAliasClosure();		String[] columns = getSubclassColumnClosure();		for ( int i = 0; i < getSubclassColumnClosure().length; i++ ) {			boolean selectable = ( allProperties || !subclassColumnLazyClosure[i] ) && 				!isSubclassTableSequentialSelect( columnTableNumbers[i] ) &&				subclassColumnSelectableClosure[i];			if ( selectable ) {				String subalias = generateTableAlias( name, columnTableNumbers[i] );				select.addColumn( subalias, columns[i], columnAliases[i] );			}		}		int[] formulaTableNumbers = getSubclassFormulaTableNumberClosure();		String[] formulaTemplates = getSubclassFormulaTemplateClosure();		String[] formulaAliases = getSubclassFormulaAliasClosure();		for ( int i = 0; i < getSubclassFormulaTemplateClosure().length; i++ ) {			boolean selectable = ( allProperties || !subclassFormulaLazyClosure[i] ) 				&& !isSubclassTableSequentialSelect( formulaTableNumbers[i] );			if ( selectable ) {				String subalias = generateTableAlias( name, formulaTableNumbers[i] );				select.addFormula( subalias, formulaTemplates[i], formulaAliases[i] );			}		}		if ( entityMetamodel.hasSubclasses() ) addDiscriminatorToSelect( select, name, suffix );		if ( hasRowId() ) select.addColumn( name, rowIdName, ROWID_ALIAS );		return select.toFragmentString();	}	public Object[] getDatabaseSnapshot(Serializable id, SessionImplementor session)			throws HibernateException {		if ( log.isTraceEnabled() ) {			log.trace( "Getting current persistent state for: " + MessageHelper.infoString( this, id, getFactory() ) );		}		try {			PreparedStatement ps = session.getBatcher().prepareSelectStatement( getSQLSnapshotSelectString() );			try {				getIdentifierType().nullSafeSet( ps, id, 1, session );				//if ( isVersioned() ) getVersionType().nullSafeSet( ps, version, getIdentifierColumnSpan()+1, session );				ResultSet rs = ps.executeQuery();				try {					//if there is no resulting row, return null					if ( !rs.next() ) return null;										//otherwise return the "hydrated" state (ie. associations are not resolved)					Type[] types = getPropertyTypes();					Object[] values = new Object[types.length];					boolean[] includeProperty = getPropertyUpdateability();					for ( int i = 0; i < types.length; i++ ) {						if ( includeProperty[i] ) {							values[i] = types[i].hydrate( rs, getPropertyAliases( "", i ), session, null ); //null owner ok??						}					}					return values;				}				finally {					rs.close();				}			}			finally {				session.getBatcher().closeStatement( ps );			}		}		catch ( SQLException sqle ) {			throw JDBCExceptionHelper.convert(					getFactory().getSQLExceptionConverter(),					sqle,					"could not retrieve snapshot: " + 					MessageHelper.infoString( this, id, getFactory() ),			        getSQLSnapshotSelectString()			);		}	}	/**	 * Generate the SQL that selects the version number by id	 */	protected String generateSelectVersionString() {		SimpleSelect select = new SimpleSelect( getFactory().getDialect() )				.setTableName( getVersionedTableName() );		if ( isVersioned() ) {			select.addColumn( versionColumnName );		}		else {			select.addColumns( rootTableKeyColumnNames );		}		if ( getFactory().getSettings().isCommentsEnabled() ) select.setComment( "get version " + getEntityName() );		return select.addCondition( rootTableKeyColumnNames, "=?" ).toStatementString();	}	protected String concretePropertySelectFragment(String alias, boolean[] includeProperty) {		int propertyCount = getPropertyNames().length;		int[] propertyTableNumbers = getPropertyTableNumbersInSelect();		SelectFragment frag = new SelectFragment();		for ( int i = 0; i < propertyCount; i++ ) {			if ( includeProperty[i] ) { //ie. updateable, not a formula				frag.addColumns( generateTableAlias( alias, propertyTableNumbers[i] ),						propertyColumnNames[i],						propertyColumnAliases[i] );				//don't need to handle formulas 'cos they aren't updateable!			}		}		return frag.toFragmentString();	}	protected String generateSnapshotSelectString() {		StringBuffer where = new StringBuffer()				.append( StringHelper.join( "=? and ",						StringHelper.qualify( getRootAlias(), getIdentifierColumnNames() ) ) )				.append( "=?" )				.append( whereJoinFragment( getRootAlias(), true, false ) );		/*if ( isVersioned() ) {			where.append(" and ")				.append( getVersionColumnName() )				.append("=?");		}*/				//TODO: should we use SELECT .. FOR UPDATE?		Select select = new Select( getFactory().getDialect() );		if ( getFactory().getSettings().isCommentsEnabled() ) {			select.setComment( "get current state " + getEntityName() );		}		return select.setSelectClause( StringHelper.join( ", ",				StringHelper.qualify( getRootAlias(), getIdentifierColumnNames() ) ) +				concretePropertySelectFragment( getRootAlias(), getPropertyUpdateability() ) )				.setFromClause( fromTableFragment( getRootAlias() ) + fromJoinFragment( getRootAlias(), true, false ) )				.setOuterJoins( "", "" )				.setWhereClause( where.toString() )				.toStatementString();	}	/**	 * Do a version check	 */	public void lock(Serializable id, Object version, Object object, LockMode lockMode, SessionImplementor session) throws HibernateException {		if ( lockMode != LockMode.NONE ) {			if ( log.isTraceEnabled() ) {				log.trace( "Locking entity: " + MessageHelper.infoString( this, id, getFactory() ) );				if ( isVersioned() ) log.trace( "Version: " + version );			}			final String sql = getLockString( lockMode );			try {				PreparedStatement st = session.getBatcher().prepareSelectStatement( sql );				try {					getIdentifierType().nullSafeSet( st, id, 1, session );					if ( isVersioned() ) {						getVersionType().nullSafeSet( st, version, getIdentifierColumnSpan() + 1, session );					}					ResultSet rs = st.executeQuery();					try {						if ( !rs.next() ) {							throw new StaleObjectStateException( getEntityName(), id );						}					}					finally {						rs.close();					}				}				finally {					session.getBatcher().closeStatement( st );				}			}			catch ( SQLException sqle ) {				throw JDBCExceptionHelper.convert(						getFactory().getSQLExceptionConverter(),						sqle,						"could not lock: " + 						MessageHelper.infoString( this, id, getFactory() ),						sql				);			}		}	}	/**	 * Retrieve the version number	 */	public Object getCurrentVersion(Serializable id, SessionImplementor session) throws HibernateException {		if ( log.isTraceEnabled() ) {			log.trace( "Getting version: " + MessageHelper.infoString( this, id, getFactory() ) );		}		try {			PreparedStatement st = session.getBatcher().prepareSelectStatement( getVersionSelectString() );			try {				getIdentifierType().nullSafeSet( st, id, 1, session );				ResultSet rs = st.executeQuery();				try {					if ( !rs.next() ) return null;					if ( !isVersioned() ) return this;					return getVersionType().nullSafeGet( rs, getVersionColumnName(), session, null );				}				finally {					rs.close();				}			}			finally {				session.getBatcher().closeStatement( st );			}		}		catch ( SQLException sqle ) {			throw JDBCExceptionHelper.convert(					getFactory().getSQLExceptionConverter(),					sqle,					"could not retrieve version: " + 					MessageHelper.infoString( this, id, getFactory() ),					getVersionSelectString()			);		}	}	/**	 * Generate the SQL that pessimistic locks a row by id (and version)	 */	protected String generateLockString(LockMode lockMode) {		SimpleSelect select = new SimpleSelect( getFactory().getDialect() )				.setLockMode( lockMode )				.setTableName( getVersionedTableName() )				.addColumn( rootTableKeyColumnNames[0] )				.addCondition( rootTableKeyColumnNames, "=?" );		if ( isVersioned() ) {			select.addWhereToken( "and" )					.addCondition( getVersionColumnName(), "=?" );		}		if ( getFactory().getSettings().isCommentsEnabled() ) {			select.setComment( "lock " + getEntityName() );		}		return select.toStatementString();	}	protected void initLockers() {		lockers.put( LockMode.READ, generateLockString( LockMode.READ ) );		lockers.put( LockMode.UPGRADE, generateLockString( LockMode.UPGRADE ) );		lockers.put( LockMode.UPGRADE_NOWAIT, generateLockString( LockMode.UPGRADE_NOWAIT ) );	}	public String[] toColumns(String alias, String propertyName) throws QueryException {		return propertyMapping.toColumns( alias, propertyName );	}	public String[] toColumns(String propertyName) throws QueryException {		return propertyMapping.getColumnNames( propertyName );	}	public Type toType(String propertyName) throws QueryException {		return propertyMapping.toType( propertyName );	}	public String[] getPropertyColumnNames(String propertyName) {		return propertyMapping.getColumnNames( propertyName );	}	/**	 * Warning:	 * When there are duplicated property names in the subclasses	 * of the class, this method may return the wrong table	 * number for the duplicated subclass property (note that	 * SingleTableEntityPersister defines an overloaded form	 * which takes the entity name.	 */	protected int getSubclassPropertyTableNumber(String propertyName) {		Type type = propertyMapping.toType(propertyName);		if ( type.isAssociationType() && ( (AssociationType) type ).useLHSPrimaryKey() ) return 0;		int index = ArrayHelper.indexOf( getSubclassPropertyNameClosure(), propertyName); //TODO: optimize this better!		return index==-1 ? 0 : getSubclassPropertyTableNumber(index);	}	protected String generateTableAlias(String rootAlias, int tableNumber) {		if ( tableNumber == 0 ) return rootAlias;		StringBuffer buf = new StringBuffer().append( rootAlias );		if ( !rootAlias.endsWith( "_" ) ) buf.append( '_' );		return buf.append( tableNumber ).append( '_' ).toString();	}	public String[] toColumns(String name, final int i) {		final String alias = generateTableAlias( name, getSubclassPropertyTableNumber( i ) );		String[] cols = getSubclassPropertyColumnNames( i );		String[] templates = getSubclassPropertyFormulaTemplateClosure()[i];		String[] result = new String[cols.length];		for ( int j = 0; j < cols.length; j++ ) {			if ( cols[j] == null ) {				result[j] = StringHelper.replace( templates[j], Template.TEMPLATE, alias );			}			else {				result[j] = StringHelper.qualify( alias, cols[j] );			}		}		return result;

⌨️ 快捷键说明

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