📄 abstractentitypersister.java
字号:
aliases[0] = prop.getFormula().getAlias(); } else { aliases = new String[ prop.getColumnSpan() ]; cols = new String[ prop.getColumnSpan() ]; Iterator colIter = prop.getColumnIterator(); int l=0; while ( colIter.hasNext() ) { Column col = (Column) colIter.next(); aliases[l] = col.getAlias(); cols[l] = col.getQuotedName(dialect); l++; } // used for loading entities by a unique key: if ( prop.getValue().isUnique() ) { uniqueKeyColumns.put( propname, cols ); } } subclassPropertyAliases.put(propname, aliases); } // aliases for identifier if ( hasIdentifierProperty() ) { subclassPropertyAliases.put( getIdentifierPropertyName(), getIdentifierAliases() ); subclassPropertyAliases.put( ENTITY_ID, getIdentifierAliases() ); } else if ( hasEmbeddedIdentifier() ) { // Fetch embedded identifiers propertynames from the "virtual" identifier component ComponentType componentId = (ComponentType) getIdentifierType(); String[] idPropertyNames = componentId.getPropertyNames(); String[] idColumns = getIdentifierColumnNames(); for (int i = 0; i < idPropertyNames.length; i++) { subclassPropertyAliases.put(idColumns[i], new String[] { idPropertyNames[i] } ); } } if ( isPolymorphic() ) subclassPropertyAliases.put( ENTITY_CLASS, new String[] { getDiscriminatorAlias() } ); } private boolean initHasCollections() { return initHasCollections(propertyTypes); } private boolean initHasCollections(Type[] types) { for ( int i=0; i<types.length; i++ ) { if ( types[i].isPersistentCollectionType() ) { return true; } else if ( types[i].isComponentType() ) { if ( initHasCollections( ( (AbstractComponentType) types[i] ).getSubtypes() ) ) return true; } } return false; } public ClassMetadata getClassMetadata() { return this; } public Class getConcreteProxyClass() { return concreteProxyClass; } public Class getMappedSuperclass() { return superclass; } public boolean isExplicitPolymorphism() { return explicitPolymorphism; } public boolean[] getPropertyUpdateability() { return propertyUpdateability; } public boolean[] getPropertyNullability() { return propertyNullability; } protected boolean useDynamicUpdate() { return dynamicUpdate; } protected boolean useDynamicInsert() { return dynamicInsert; } public boolean[] getPropertyInsertability() { return propertyInsertability; } public Object getPropertyValue(Object object, String propertyName) throws HibernateException { Getter getter = (Getter) gettersByPropertyName.get(propertyName); if (getter==null) throw new HibernateException("unmapped property: " + propertyName); return getter.get(object); } public void setPropertyValue(Object object, String propertyName, Object value) throws HibernateException { Setter setter = (Setter) settersByPropertyName.get(propertyName); if (setter==null) throw new HibernateException("unmapped property: " + propertyName); setter.set(object, value); } protected boolean hasEmbeddedIdentifier() { return hasEmbeddedIdentifier; } public boolean[] getNotNullInsertableColumns(Object[] fields) { boolean[] notNull = new boolean[fields.length]; boolean[] insertable = getPropertyInsertability(); for ( int i=0; i<fields.length; i++ ) notNull[i] = insertable[i] && fields[i]!=null; return notNull; } protected Dialect getDialect() { return dialect; } protected String getSQLWhereString(String alias) { return StringHelper.replace(sqlWhereStringTemplate, Template.TEMPLATE, alias); } protected boolean hasWhere() { return sqlWhereString!=null; } public boolean hasIdentifierPropertyOrEmbeddedCompositeIdentifier() { return hasIdentifierProperty() || hasEmbeddedIdentifier; } protected void checkColumnDuplication(Set distinctColumns, Iterator columns) throws MappingException { while ( columns.hasNext() ) { Column col = (Column) columns.next(); if ( !distinctColumns.add( col.getName() ) ) throw new MappingException( "Repeated column in mapping for class " + className + " should be mapped with insert=\"false\" update=\"false\": " + col.getName() ); } } protected UniqueEntityLoader createEntityLoader(SessionFactoryImplementor factory) throws MappingException { Loader nonBatchLoader = new EntityLoader(this, 1, factory); if (batchSize>1) { Loader batchLoader = new EntityLoader(this, batchSize, factory); int smallBatchSize = (int) Math.round( Math.sqrt(batchSize) ); Loader smallBatchLoader = new EntityLoader(this, smallBatchSize, factory); return new BatchingEntityLoader(this, batchSize, batchLoader, smallBatchSize, smallBatchLoader, nonBatchLoader); } else { return (UniqueEntityLoader) nonBatchLoader; } } protected void createUniqueKeyLoaders(SessionFactoryImplementor factory) throws MappingException { //TODO: does not handle components, or properties of a joined subclass for ( int i=0; i<propertyNames.length; i++ ) { String[] columns = (String[]) uniqueKeyColumns.get( propertyNames[i] ); if (columns!=null) { Type uniqueKeyType = propertyTypes[i]; if ( uniqueKeyType.isEntityType() ) { Class clazz = ( (EntityType) uniqueKeyType ).getAssociatedClass(); uniqueKeyType = factory.getPersister(clazz).getIdentifierType(); } uniqueKeyLoaders.put( propertyNames[i], new EntityLoader(this, columns, uniqueKeyType, 1, factory) ); } } } public Type getType() { return entityType; } protected int getHydrateSpan() { return hydrateSpan; } public boolean isBatchLoadable() { return batchSize>1; } 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[] getJoinKeyColumnNames() { return getIdentifierColumnNames(); } public String getName() { return getClassName(); } public String joinSelectFragment(String alias, String suffix) { return identifierSelectFragment(alias, suffix) + propertySelectFragment(alias, suffix); } 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( getPropertyColumnNames(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 hasSubclasses() ? new Alias(suffix).toAliasString( getDiscriminatorAlias() ) : null; } protected abstract String getDiscriminatorAlias(); public Object loadByUniqueKey(String propertyName, Serializable uniqueKey, SessionImplementor session) throws HibernateException, SQLException { return ( (EntityLoader) uniqueKeyLoaders.get(propertyName) ).loadByUniqueKey(session, uniqueKey); } public String[] getUniqueKeyColumnNames(String propertyName) { return (String[]) uniqueKeyColumns.get(propertyName); } public boolean isCollection() { return false; } public boolean consumesAlias() { return true; } public Type getPropertyType(String propertyName) throws MappingException { Type propertyType = (Type) typesByPropertyName.get(propertyName); if (propertyType==null) throw new MappingException("property does not exist: " + propertyName); return propertyType; } protected boolean hasSelectBeforeUpdate() { return selectBeforeUpdate; } protected abstract String getVersionSelectString(); /** * Retrieve the version number */ public Object getCurrentVersion(Serializable id, SessionImplementor session) throws HibernateException { if ( log.isTraceEnabled() ) { log.trace( "Getting version: " + MessageHelper.infoString(this, id) ); } try { PreparedStatement st = session.getBatcher().prepareStatement( 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(); } } catch(SQLException sqle) { JDBCExceptionReporter.logExceptions(sqle); throw sqle; } finally { session.getBatcher().closeStatement(st); } } catch (SQLException sqle) { throw new JDBCException("could not retrieve version: " + MessageHelper.infoString(this, id), sqle ); } } public Object[] getCurrentPersistentState(Serializable id, Object version, SessionImplementor session) throws HibernateException { if ( !hasSelectBeforeUpdate() ) return null; if ( log.isTraceEnabled() ) log.trace( "Getting current persistent state for: " + MessageHelper.infoString(this, id) ); Type[] types = getPropertyTypes(); Object[] values = new Object[ types.length ]; boolean[] includeProperty = getPropertyUpdateability(); try { PreparedStatement ps = session.getBatcher().prepareQueryStatement( getConcreteSelectString(), false ); ResultSet rs = null; try { getIdentifierType().nullSafeSet(ps, id, 1, session); getVersionType().nullSafeSet( ps, version, getIdentifierColumnNames().length+1, session ); rs = session.getBatcher().getResultSet(ps); if ( !rs.next() ) throw new StaleObjectStateException( getMappedClass(), id ); for (int i=0; i<types.length; i++) { if ( includeProperty[i] ) { values[i] = types[i].hydrate( rs, getPropertyAliases(StringHelper.EMPTY_STRING, i), session, null ); //null owner ok?? } } } catch(SQLException sqle) { JDBCExceptionReporter.logExceptions(sqle); throw sqle; } finally { session.getBatcher().closeQueryStatement(ps, rs); } } catch (SQLException sqle) { throw new JDBCException(sqle); } return values; } /** * Generate the SQL that selects the version number by id */ protected String generateSelectVersionString() { SimpleSelect select = new SimpleSelect() .setTableName( getTableName() ); if ( isVersioned() ) { select.addColumn( getVersionColumnName() ); } else { select.addColumns( getIdentifierColumnNames() ); } return select.addCondition( getIdentifierColumnNames(), "=?" ).toStatementString(); } protected abstract String getConcreteSelectString(); protected final int optimisticLockMode() { return optimisticLockMode; } public boolean isManyToMany() { return false; } public Object createProxy(Serializable id, SessionImplementor session) throws HibernateException { return CGLIBLazyInitializer.getProxy( proxyFactory, mappedClass, proxyInterfaces, proxyGetIdentifierMethod, proxySetIdentifierMethod, id, session ); } /** * Transform the array of property indexes to an array of booleans */ protected final boolean[] getPropertiesToUpdate(final int[] dirtyProperties) { final boolean[] propsToUpdate = new boolean[ getHydrateSpan() ]; for (int j=0; j<dirtyProperties.length; j++) { propsToUpdate[ dirtyProperties[j] ] = true; } if ( isVersioned() ) propsToUpdate[ getVersionProperty() ] = true; return propsToUpdate; } public String toString() { return getClass().getName() + " for class: " + className; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -