abstractcollectionpersister.java
来自「hibernate-3.0.5 中文文档」· Java 代码 · 共 1,293 行 · 第 1/3 页
JAVA
1,293 行
if ( elementType.isComponentType() ) { elementPropertyMapping = new CompositeElementPropertyMapping( elementColumnNames, elementFormulaTemplates, (AbstractComponentType) elementType, factory ); } else if ( !elementType.isEntityType() ) { elementPropertyMapping = new ElementPropertyMapping( elementColumnNames, elementType ); } else { if ( elementPersister instanceof PropertyMapping ) { //not all classpersisters implement PropertyMapping! elementPropertyMapping = (PropertyMapping) elementPersister; } else { elementPropertyMapping = new ElementPropertyMapping( elementColumnNames, elementType ); } } // Handle any filters applied to this collection filterHelper = new FilterHelper( collection.getFilterMap(), dialect ); // Handle any filters applied to this collection for many-to-many manyToManyFilterHelper = new FilterHelper( collection.getManyToManyFilterMap(), dialect ); manyToManyWhereString = collection.getManyToManyWhere(); manyToManyWhereTemplate = manyToManyWhereString == null ? null : Template.renderWhereStringTemplate( manyToManyWhereString, factory.getDialect() ); } public void postInstantiate() throws MappingException { initializer = queryLoaderName == null ? createCollectionInitializer( CollectionHelper.EMPTY_MAP ) : new NamedQueryCollectionInitializer( queryLoaderName, this ); } protected void logStaticSQL() { if ( log.isDebugEnabled() ) { log.debug( "Static SQL for collection: " + getRole() ); if ( getSQLInsertRowString() != null ) log.debug( " Row insert: " + getSQLInsertRowString() ); if ( getSQLUpdateRowString() != null ) log.debug( " Row update: " + getSQLUpdateRowString() ); if ( getSQLDeleteRowString() != null ) log.debug( " Row delete: " + getSQLDeleteRowString() ); if ( getSQLDeleteString() != null ) log.debug( " One-shot delete: " + getSQLDeleteString() ); } } public void initialize(Serializable key, SessionImplementor session) throws HibernateException { getAppropriateInitializer( key, session ).initialize( key, session ); } protected CollectionInitializer getAppropriateInitializer(Serializable key, SessionImplementor session) { if ( queryLoaderName != null ) { //if there is a user-specified loader, return that //TODO: filters!? return initializer; } CollectionInitializer subselectInitializer = getSubselectInitializer( key, session ); if ( subselectInitializer != null ) { return subselectInitializer; } else if ( session.getEnabledFilters().isEmpty() ) { return initializer; } else { return createCollectionInitializer( session.getEnabledFilters() ); } } private CollectionInitializer getSubselectInitializer(Serializable key, SessionImplementor session) { if ( !isSubselectLoadable() ) return null; final PersistenceContext persistenceContext = session.getPersistenceContext(); SubselectFetch subselect = persistenceContext.getBatchFetchQueue() .getSubselect( new EntityKey( key, getOwnerEntityPersister(), session.getEntityMode() ) ); if (subselect == null) { return null; } else { // Take care of any entities that might have // been evicted! Iterator iter = subselect.getResult().iterator(); while ( iter.hasNext() ) { if ( !persistenceContext.containsEntity( (EntityKey) iter.next() ) ) { iter.remove(); } } // Run a subquery loader return createSubselectInitializer( subselect, session ); } } protected abstract CollectionInitializer createSubselectInitializer(SubselectFetch subselect, SessionImplementor session); protected abstract CollectionInitializer createCollectionInitializer(Map enabledFilters) throws MappingException; public CacheConcurrencyStrategy getCache() { return cache; } public boolean hasCache() { return cache != null; } public CollectionType getCollectionType() { return collectionType; } protected String getSQLWhereString(String alias) { return StringHelper.replace( sqlWhereStringTemplate, Template.TEMPLATE, alias ); } public String getSQLOrderByString(String alias) { return hasOrdering() ? StringHelper.replace( sqlOrderByStringTemplate, Template.TEMPLATE, alias ) : ""; } public FetchMode getFetchMode() { return fetchMode; } public boolean hasOrdering() { return hasOrder; } public boolean hasWhere() { return hasWhere; } protected String getSQLDeleteString() { return sqlDeleteString; } protected String getSQLInsertRowString() { return sqlInsertRowString; } protected String getSQLUpdateRowString() { return sqlUpdateRowString; } protected String getSQLDeleteRowString() { return sqlDeleteRowString; } public Type getKeyType() { return keyType; } public Type getIndexType() { return indexType; } public Type getElementType() { return elementType; } /** * Return the element class of an array, or null otherwise */ public Class getElementClass() { //needed by arrays return elementClass; } public Object readElement(ResultSet rs, Object owner, String[] aliases, SessionImplementor session) throws HibernateException, SQLException { Object element = getElementType().nullSafeGet( rs, aliases, session, owner ); return element; } public Object readIndex(ResultSet rs, String[] aliases, SessionImplementor session) throws HibernateException, SQLException { Object index = getIndexType().nullSafeGet( rs, aliases, session, null ); if ( index == null ) { throw new HibernateException( "null index column for collection: " + role ); } if (baseIndex!=0) { index = new Integer( ( (Integer) index ).intValue() - baseIndex ); } return index; } public Object readIdentifier(ResultSet rs, String alias, SessionImplementor session) throws HibernateException, SQLException { Object id = getIdentifierType().nullSafeGet( rs, alias, session, null ); if ( id == null ) throw new HibernateException( "null identifier column for collection: " + role ); return id; } public Object readKey(ResultSet rs, String[] aliases, SessionImplementor session) throws HibernateException, SQLException { return getKeyType().nullSafeGet( rs, aliases, session, null ); } /** * Write the key to a JDBC <tt>PreparedStatement</tt> */ protected int writeKey(PreparedStatement st, Serializable key, int i, SessionImplementor session) throws HibernateException, SQLException { if ( key == null ) throw new NullPointerException( "null key for collection: " + role ); //an assertion getKeyType().nullSafeSet( st, key, i, session ); return i + keyColumnAliases.length; } /** * Write the element to a JDBC <tt>PreparedStatement</tt> */ protected int writeElement(PreparedStatement st, Object elt, int i, SessionImplementor session) throws HibernateException, SQLException { getElementType().nullSafeSet(st, elt, i, elementColumnIsSettable, session); return i + ArrayHelper.countTrue(elementColumnIsSettable); } /** * Write the index to a JDBC <tt>PreparedStatement</tt> */ protected int writeIndex(PreparedStatement st, Object index, int i, SessionImplementor session) throws HibernateException, SQLException { if (baseIndex!=0) { index = new Integer( ( (Integer) index ).intValue() + baseIndex ); } getIndexType().nullSafeSet( st, index, i, indexColumnIsSettable, session ); return i + ArrayHelper.countTrue(indexColumnIsSettable); } /** * Write the element to a JDBC <tt>PreparedStatement</tt> */ protected int writeElementToWhere(PreparedStatement st, Object elt, int i, SessionImplementor session) throws HibernateException, SQLException { if (elementIsFormula) throw new AssertionFailure("cannot use a formula-based element in the where condition"); getElementType().nullSafeSet(st, elt, i, elementColumnIsNotNullable, session); return i + elementColumnAliases.length; } /** * Write the index to a JDBC <tt>PreparedStatement</tt> */ protected int writeIndexToWhere(PreparedStatement st, Object index, int i, SessionImplementor session) throws HibernateException, SQLException { if (indexIsFormula) throw new AssertionFailure("cannot use a formula-based index in the where condition"); if (baseIndex!=0) { index = new Integer( ( (Integer) index ).intValue() + baseIndex ); } getIndexType().nullSafeSet( st, index, i, session ); return i + indexColumnAliases.length; } /** * Write the identifier to a JDBC <tt>PreparedStatement</tt> */ public int writeIdentifier(PreparedStatement st, Object id, int i, SessionImplementor session) throws HibernateException, SQLException { getIdentifierType().nullSafeSet( st, id, i, session ); return i + 1; } public boolean isPrimitiveArray() { return isPrimitiveArray; } public boolean isArray() { return isArray; } public String[] getKeyColumnAliases(String suffix) { return new Alias( suffix ).toAliasStrings( keyColumnAliases ); } public String[] getElementColumnAliases(String suffix) { return new Alias( suffix ).toAliasStrings( elementColumnAliases ); } public String[] getIndexColumnAliases(String suffix) { if ( hasIndex ) { return new Alias( suffix ).toAliasStrings( indexColumnAliases ); } else { return null; } } public String getIdentifierColumnAlias(String suffix) { if ( hasIdentifier ) { return new Alias( suffix ).toAliasString( identifierColumnAlias ); } else { return null; } } /** * Generate a list of collection index, key and element columns */ public String selectFragment(String alias, String columnSuffix) { SelectFragment frag = generateSelectFragment( alias, columnSuffix ); appendElementColumns( frag, alias ); appendIndexColumns( frag, alias ); appendIdentifierColumns( frag, alias ); return frag.toFragmentString() .substring( 2 ); //strip leading ',' } protected SelectFragment generateSelectFragment(String alias, String columnSuffix) { SelectFragment frag = new SelectFragment() .setSuffix( columnSuffix ) .addColumns( alias, keyColumnNames, keyColumnAliases ); return frag; } protected void appendElementColumns(SelectFragment frag, String elemAlias) { for ( int i=0; i<elementColumnIsSettable.length; i++ ) { if ( elementColumnIsSettable[i] ) { frag.addColumn( elemAlias, elementColumnNames[i], elementColumnAliases[i] ); } else { frag.addFormula( elemAlias, elementFormulaTemplates[i], elementColumnAliases[i] ); } } } protected void appendIndexColumns(SelectFragment frag, String alias) { if ( hasIndex ) { for ( int i=0; i<indexColumnIsSettable.length; i++ ) { if ( indexColumnIsSettable[i] ) { frag.addColumn( alias, indexColumnNames[i], indexColumnAliases[i] ); } else { frag.addFormula( alias, indexFormulaTemplates[i], indexColumnAliases[i] ); } } } } protected void appendIdentifierColumns(SelectFragment frag, String alias) { if ( hasIdentifier ) frag.addColumn( alias, identifierColumnName, identifierColumnAlias ); } public String[] getIndexColumnNames() { return indexColumnNames; } public String[] getIndexColumnNames(String alias) { return indexIsFormula ? StringHelper.replace( indexFormulaTemplates, Template.TEMPLATE, alias ) : StringHelper.qualify(alias, indexColumnNames); } public String[] getElementColumnNames(String alias) { return elementIsFormula ? StringHelper.replace( elementFormulaTemplates, Template.TEMPLATE, alias ) : StringHelper.qualify(alias, elementColumnNames); } public String[] getElementColumnNames() { return elementColumnNames; //TODO: something with formulas... } public String[] getKeyColumnNames() { return keyColumnNames; } public boolean hasIndex() { return hasIndex; } public boolean isLazy() { return isLazy; } public boolean isInverse() { return isInverse; } public String getTableName() { return qualifiedTableName; } public void remove(Serializable id, SessionImplementor session) throws HibernateException { if ( !isInverse && isRowDeleteEnabled() ) { if ( log.isDebugEnabled() ) { log.debug( "Deleting collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) ); } // Remove all the old entries try { int offset = 1; PreparedStatement st = null; if ( isDeleteCallable() ) { CallableStatement callstatement = session.getBatcher() .prepareBatchCallableStatement( getSQLDeleteString() ); callstatement.registerOutParameter( offset++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ? st = callstatement; } else { st = session.getBatcher().prepareBatchStatement( getSQLDeleteString() ); } try { writeKey( st, id, offset, session ); session.getBatcher().addToBatch( -1 ); } catch ( SQLException sqle ) { session.getBatcher().abortBatch( sqle ); throw sqle; } if ( log.isDebugEnabled() ) log.debug( "done deleting collection" ); } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( sqlExceptionConverter, sqle,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?