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

📄 basicentitypersister.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	}	private int getSubclassPropertyIndex(String propertyName) {		return ArrayHelper.indexOf(subclassPropertyNameClosure, propertyName);	}		protected String[] getPropertySubclassNames() {		return propertySubclassNames;	}	private String[] getPropertyColumnNames(int i) {		return propertyColumnNames[i];	}	protected int getPropertyColumnSpan(int i) {		return propertyColumnSpans[i];	}	protected boolean hasFormulaProperties() {		return hasFormulaProperties;	}	public FetchMode getFetchMode(int i) {		return subclassPropertyFetchModeClosure[i];	}	public Type getSubclassPropertyType(int i) {		return subclassPropertyTypeClosure[i];	}	public String getSubclassPropertyName(int i) {		return subclassPropertyNameClosure[i];	}	public int countSubclassProperties() {		return subclassPropertyTypeClosure.length;	}	public String[] getSubclassPropertyColumnNames(int i) {		return subclassPropertyColumnNameClosure[i];	}	public boolean isDefinedOnSubclass(int i) {		return propertyDefinedOnSubclass[i];	}	protected String[][] getSubclassPropertyFormulaTemplateClosure() {		return subclassPropertyFormulaTemplateClosure;	}	protected Type[] getSubclassPropertyTypeClosure() {		return subclassPropertyTypeClosure;	}	protected String[][] getSubclassPropertyColumnNameClosure() {		return subclassPropertyColumnNameClosure;	}	protected String[] getSubclassPropertyNameClosure() {		return subclassPropertyNameClosure;	}	protected String[] getSubclassPropertySubclassNameClosure() {		return subclassPropertySubclassNameClosure;	}	protected String[] getSubclassColumnClosure() {		return subclassColumnClosure;	}	protected String[] getSubclassColumnAliasClosure() {		return subclassColumnAliasClosure;	}	protected String[] getSubclassFormulaClosure() {		return subclassFormulaClosure;	}	protected String[] getSubclassFormulaTemplateClosure() {		return subclassFormulaTemplateClosure;	}	protected String[] getSubclassFormulaAliasClosure() {		return subclassFormulaAliasClosure;	}	public String[] getSubclassPropertyColumnAliases(String propertyName, String suffix) {		String rawAliases[] = ( String[] ) subclassPropertyAliases.get( propertyName );		if ( rawAliases == null ) return null;		String result[] = new String[rawAliases.length];		for ( int i = 0; i < rawAliases.length; i++ ) {			result[i] = new Alias( suffix ).toUnquotedAliasString( rawAliases[i] );		}		return result;	}		public String[] getSubclassPropertyColumnNames(String propertyName) {		//TODO: should we allow suffixes on these ?		return ( String[] ) subclassPropertyColumnNames.get( propertyName );	}					//This is really ugly, but necessary:	/**	 * Must be called by subclasses, at the end of their constructors	 */	protected void initSubclassPropertyAliasesMap(PersistentClass model) throws MappingException {		// ALIASES		internalInitSubclassPropertyAliasesMap( null, model.getSubclassPropertyClosureIterator() );				// aliases for identifier ( alias.id )		subclassPropertyAliases.put( ENTITY_ID, getIdentifierAliases() );		subclassPropertyColumnNames.put( ENTITY_ID, getIdentifierColumnNames() );				// aliases named identifier ( alias.idname ) 		if ( hasIdentifierProperty() ) {			subclassPropertyAliases.put( getIdentifierPropertyName(), getIdentifierAliases() );			subclassPropertyColumnNames.put( getIdentifierPropertyName(), getIdentifierColumnNames() );		}		// aliases for composite-id's  		if ( getIdentifierType().isComponentType() ) {			// Fetch embedded identifiers propertynames from the "virtual" identifier component			AbstractComponentType componentId = ( AbstractComponentType ) getIdentifierType();			String[] idPropertyNames = componentId.getPropertyNames();			String[] idAliases = getIdentifierAliases();			String[] idColumnNames = getIdentifierColumnNames();						for ( int i = 0; i < idPropertyNames.length; i++ ) {				subclassPropertyAliases.put( ENTITY_ID + "." + idPropertyNames[i], new String[]{idAliases[i]} );				subclassPropertyColumnNames.put( ENTITY_ID + "." + getIdentifierPropertyName() + "." + idPropertyNames[i],  new String[]{idColumnNames[i]} );				if (hasIdentifierProperty() && !ENTITY_ID.equals(getIdentifierPropertyName()) ) {					subclassPropertyAliases.put( getIdentifierPropertyName() + "." + idPropertyNames[i], new String[]{idAliases[i]} );					subclassPropertyColumnNames.put( getIdentifierPropertyName() + "." + idPropertyNames[i],  new String[]{idColumnNames[i]} );				} else {					// embedded composite ids ( alias.idname1, alias.idname2 )					subclassPropertyAliases.put( idPropertyNames[i], new String[]{idAliases[i]} );					subclassPropertyColumnNames.put( idPropertyNames[i],  new String[]{idColumnNames[i]} );				}			}		}		if ( entityMetamodel.isPolymorphic() ) {			subclassPropertyAliases.put( ENTITY_CLASS,					new String[]{getDiscriminatorAlias()} );			subclassPropertyColumnNames.put( ENTITY_CLASS,					new String[]{getDiscriminatorColumnName()} );		}	}	private void internalInitSubclassPropertyAliasesMap(String path, Iterator propertyIterator) {		while ( propertyIterator.hasNext() ) {			Property prop = ( Property ) propertyIterator.next();			String propname = path == null ? prop.getName() : path + "." + prop.getName();			if ( prop.isComposite() ) {				Component component = ( Component ) prop.getValue();				Iterator compProps = component.getPropertyIterator();				internalInitSubclassPropertyAliasesMap( propname, compProps );			}			else {				String[] aliases = new String[prop.getColumnSpan()];				String[] cols = new String[prop.getColumnSpan()];				Iterator colIter = prop.getColumnIterator();				int l = 0;				while ( colIter.hasNext() ) {					Selectable thing = ( Selectable ) colIter.next();					aliases[l] = thing.getAlias( prop.getValue().getTable() );					cols[l] = thing.getText( getFactory().getDialect() ); // TODO: skip formulas?					l++;				}				subclassPropertyAliases.put( propname, aliases );				subclassPropertyColumnNames.put( propname, cols );			}		}	}	public Object loadByUniqueKey(String propertyName, Object uniqueKey, SessionImplementor session)			throws HibernateException {		return getAppropriateUniqueKeyLoader( propertyName, session.getEnabledFilters() )				.loadByUniqueKey( session, uniqueKey );	}	private EntityLoader getAppropriateUniqueKeyLoader(String propertyName, Map enabledFilters) {		if ( enabledFilters == null || enabledFilters.isEmpty() ) {			return ( EntityLoader ) uniqueKeyLoaders.get( propertyName );		}		else {			int propertyIndex = getPropertyIndex( propertyName );			return createUniqueKeyLoader( getPropertyTypes()[propertyIndex],					getPropertyColumnNames( propertyIndex ),					enabledFilters );		}	}	protected Integer getPropertyIndexOrNull(String propertyName) {		return (Integer) propertyIndexes.get(propertyName);	}	public int getPropertyIndex(String propertyName) {		Integer result = getPropertyIndexOrNull(propertyName);		if (result==null) {			throw new HibernateException("could not resolve property-ref: " + propertyName);		}		return result.intValue();	}	protected void createUniqueKeyLoaders() throws MappingException {		//TODO: does not handle components		Type[] propertyTypes = getPropertyTypes();		String[] propertyNames = getPropertyNames();		for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {			if ( propertyUniqueness[i] ) {				//don't need filters for the static loaders				uniqueKeyLoaders.put( propertyNames[i],						createUniqueKeyLoader( propertyTypes[i],								getPropertyColumnNames( i ),								CollectionHelper.EMPTY_MAP ) );			}		}	}	private EntityLoader createUniqueKeyLoader(Type uniqueKeyType, String[] columns, Map enabledFilters) {		if ( uniqueKeyType.isEntityType() ) {			String className = ( ( EntityType ) uniqueKeyType ).getAssociatedEntityName();			uniqueKeyType = getFactory().getEntityPersister( className ).getIdentifierType();		}		return new EntityLoader( this, columns, uniqueKeyType, 1, LockMode.NONE, getFactory(), enabledFilters );	}	protected String getSQLWhereString(String alias) {		return StringHelper.replace( sqlWhereStringTemplate, Template.TEMPLATE, alias );	}	protected boolean hasWhere() {		return sqlWhereString != null;	}	private void initOrdinaryPropertyPaths(Mapping mapping) throws MappingException {		for ( int i = 0; i < getSubclassPropertyNameClosure().length; i++ ) {			propertyMapping.initPropertyPaths( getSubclassPropertyNameClosure()[i],					getSubclassPropertyTypeClosure()[i],					getSubclassPropertyColumnNameClosure()[i],					getSubclassPropertyFormulaTemplateClosure()[i],					mapping );		}	}	private void initIdentifierPropertyPaths(Mapping mapping) throws MappingException {		String idProp = getIdentifierPropertyName();		if ( idProp != null ) {			propertyMapping.initPropertyPaths( idProp, getIdentifierType(), getIdentifierColumnNames(), null, mapping );		}		if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {			propertyMapping.initPropertyPaths( null, getIdentifierType(), getIdentifierColumnNames(), null, mapping );		}		propertyMapping.initPropertyPaths( ENTITY_ID, getIdentifierType(), getIdentifierColumnNames(), null, mapping );	}	private void initDiscriminatorPropertyPath(Mapping mapping) throws MappingException {		propertyMapping.initPropertyPaths( ENTITY_CLASS,				getDiscriminatorType(),				new String[]{getDiscriminatorColumnName()},				new String[]{getDiscriminatorFormulaTemplate()},				getFactory() );	}	protected void initPropertyPaths(Mapping mapping) throws MappingException {		initOrdinaryPropertyPaths(mapping);		initOrdinaryPropertyPaths(mapping); //do two passes, for collection property-ref!		initIdentifierPropertyPaths(mapping);		if ( entityMetamodel.isPolymorphic() ) initDiscriminatorPropertyPath(mapping);	}	protected UniqueEntityLoader createEntityLoader(LockMode lockMode, Map enabledFilters) throws MappingException {		//TODO: disable batch loading if lockMode > READ?		return BatchingEntityLoader.createBatchingEntityLoader( this, batchSize, lockMode, getFactory(), enabledFilters );	}	protected UniqueEntityLoader createEntityLoader(LockMode lockMode) throws MappingException {		return createEntityLoader( lockMode, CollectionHelper.EMPTY_MAP );	}	protected boolean check(int rows, Serializable id, int tableNumber) throws HibernateException {		if ( rows < 1 ) {			if ( !isNullableTable( tableNumber ) ) {				throw new StaleObjectStateException( getEntityName(), id );			}		}		else if ( rows > 1 ) {			throw new HibernateException( 					"Duplicate identifier in table for: " + 					MessageHelper.infoString( this, id, getFactory() ) 			);		}		return rows > 0; //it could be zero if we have a "nullable" table	}	protected String generateUpdateString(boolean[] includeProperty, int j, boolean useRowId) {		return generateUpdateString( includeProperty, j, null, useRowId );	}	/**	 * Generate the SQL that updates a row by id (and version)	 */	protected String generateUpdateString(final boolean[] includeProperty,										  final int j,										  final Object[] oldFields,										  final boolean useRowId) {		Update update = new Update().setTableName( getTableName( j ) );				// select the correct row by either pk or rowid		if ( useRowId ) {			update.setPrimaryKeyColumnNames( new String[]{rowIdName} ); //TODO: eventually, rowIdName[j]		}		else {			update.setPrimaryKeyColumnNames( getKeyColumns( j ) );		}		boolean hasColumns = false;		for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {			if ( includeProperty[i] && isPropertyOfTable( i, j ) ) {				// this is a property of the table, which we are updating				update.addColumns( getPropertyColumnNames(i), propertyColumnUpdateable[i] );				hasColumns = hasColumns || getPropertyColumnSpan( i ) > 0;			}		}		if ( j == 0 && isVersioned() && entityMetamodel.getOptimisticLockMode() == Versioning.OPTIMISTIC_LOCK_VERSION ) {			// this is the root (versioned) table, and we 			// are using version-based optimistic locking			if ( includeProperty[ getVersionProperty() ] ) { //if we are not updating the version, also don't check it!				update.setVersionColumnName( getVersionColumnName() );				hasColumns = true;			}		}		else if ( entityMetamodel.getOptimisticLockMode() > Versioning.OPTIMISTIC_LOCK_VERSION && oldFields != null ) {			// we are using "all" or "dirty" property-based optimistic locking						boolean[] includeInWhere = entityMetamodel.getOptimisticLockMode() == Versioning.OPTIMISTIC_LOCK_ALL ?					getPropertyUpdateability() : //optimistic-lock="all", include all updatable properties					includeProperty; //optimistic-lock="dirty", include all properties we are updating this time			for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {				boolean[] versionability = getPropertyVersionability();				if ( includeInWhere[i] && isPropertyOfTable( i, j ) && versionability[i] ) {					// this property belongs to the table, and it is not specifically 					// excluded from optimistic locking by optimistic-lock="false"					if ( oldFields[i] == null ) {						update.addWhereColumns( getPropertyColumnNames( i ), " is null" );					}					else {						update.addWhereColumns( getPropertyColumnNames( i ) );					}				}			}		}		if ( getFactory().getSettings().isCommentsEnabled() ) {			update.setComment( "update " + getEntityName() );		}		return hasColumns ? update.toStatementString() : null;	}	protected String generateInsertString(boolean[] includeProperty, int j) {		return generateInsertString( false, includeProperty, j );	}	protected String generateInsertString(boolean identityInsert, boolean[] includeProperty) {		return generateInsertString( identityInsert, includeProperty, 0 );	}	/**	 * Generate the SQL that inserts a row

⌨️ 快捷键说明

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