📄 abstractcollectionpersister.java
字号:
? ExecuteUpdateResultCheckStyle.determineDefault( collection.getCustomSQLUpdate(), insertCallable ) : collection.getCustomSQLUpdateCheckStyle(); } if ( collection.getCustomSQLDelete() == null ) { sqlDeleteRowString = generateDeleteRowString(); deleteCallable = false; deleteCheckStyle = ExecuteUpdateResultCheckStyle.NONE; } else { sqlDeleteRowString = collection.getCustomSQLDelete(); deleteCallable = collection.isCustomDeleteCallable(); deleteCheckStyle = ExecuteUpdateResultCheckStyle.NONE; } if ( collection.getCustomSQLDeleteAll() == null ) { sqlDeleteString = generateDeleteString(); deleteAllCallable = false; deleteAllCheckStyle = ExecuteUpdateResultCheckStyle.NONE; } else { sqlDeleteString = collection.getCustomSQLDeleteAll(); deleteAllCallable = collection.isCustomDeleteAllCallable(); deleteAllCheckStyle = ExecuteUpdateResultCheckStyle.NONE; } sqlSelectSizeString = generateSelectSizeString( collection.isIndexed() && !collection.isMap() ); sqlDetectRowByIndexString = generateDetectRowByIndexString(); sqlDetectRowByElementString = generateDetectRowByElementString(); sqlSelectRowByIndexString = generateSelectRowByIndexString(); logStaticSQL(); isLazy = collection.isLazy(); isExtraLazy = collection.isExtraLazy(); isInverse = collection.isInverse(); if ( collection.isArray() ) { elementClass = ( (org.hibernate.mapping.Array) collection ).getElementClass(); } else { // for non-arrays, we don't need to know the element class elementClass = null; //elementType.returnedClass(); } 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, factory.getSqlFunctionRegistry() ); // Handle any filters applied to this collection for many-to-many manyToManyFilterHelper = new FilterHelper( collection.getManyToManyFilterMap(), dialect, factory.getSqlFunctionRegistry() ); manyToManyWhereString = StringHelper.isNotEmpty( collection.getManyToManyWhere() ) ? "( " + collection.getManyToManyWhere() + ")" : null; manyToManyWhereTemplate = manyToManyWhereString == null ? null : Template.renderWhereStringTemplate( manyToManyWhereString, factory.getDialect(), factory.getSqlFunctionRegistry() ); manyToManyOrderByString = collection.getManyToManyOrdering(); manyToManyOrderByTemplate = manyToManyOrderByString == null ? null : Template.renderOrderByStringTemplate( manyToManyOrderByString, factory.getDialect(), factory.getSqlFunctionRegistry() ); initCollectionPropertyMap(); } 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 CollectionRegionAccessStrategy getCacheAccessStrategy() { return cacheAccessStrategy; } public boolean hasCache() { return cacheAccessStrategy != 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 String getManyToManyOrderByString(String alias) { if ( isManyToMany() && manyToManyOrderByString != null ) { return StringHelper.replace( manyToManyOrderByTemplate, Template.TEMPLATE, alias ); } else { return ""; } } public FetchMode getFetchMode() { return fetchMode; } public boolean hasOrdering() { return hasOrder; } public boolean hasManyToManyOrdering() { return isManyToMany() && manyToManyOrderByTemplate != null; } 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 { return getElementType().nullSafeGet( rs, aliases, session, owner ); } 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 ); } index = decrementIndexByBase( index ); return index; } protected Object decrementIndexByBase(Object index) { 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 { getIndexType().nullSafeSet( st, incrementIndexByBase(index), i, indexColumnIsSettable, session ); return i + ArrayHelper.countTrue(indexColumnIsSettable); } protected Object incrementIndexByBase(Object index) { if (baseIndex!=0) { index = new Integer( ( (Integer) index ).intValue() + baseIndex ); } return index; } /** * Write the element to a JDBC <tt>PreparedStatement</tt> */ protected int writeElementToWhere(PreparedStatement st, Object elt, int i, SessionImplementor session) throws HibernateException, SQLException { if (elementIsPureFormula) { throw new AssertionFailure("cannot use a formula-based element in the where condition"); } getElementType().nullSafeSet(st, elt, i, elementColumnIsInPrimaryKey, 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 (indexContainsFormula) { throw new AssertionFailure("cannot use a formula-based index in the where condition"); } getIndexType().nullSafeSet( st, incrementIndexByBase(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; } } public String getIdentifierColumnName() { if ( hasIdentifier ) { return identifierColumnName; } 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 String generateSelectSizeString(boolean isIntegerIndexed) { String selectValue = isIntegerIndexed ? "max(" + getIndexColumnNames()[0] + ") + 1": //lists, arrays "count(" + getElementColumnNames()[0] + ")"; //sets, maps, bags return new SimpleSelect(dialect) .setTableName( getTableName() ) .addCondition( getKeyColumnNames(), "=?" ) .addColumn(selectValue) .toStatementString(); } protected String generateDetectRowByIndexString() { if ( !hasIndex() ) { return null; } return new SimpleSelect(dialect) .setTableName( getTableName() ) .addCondition( getKeyColumnNames(), "=?" ) .addCondition( getIndexColumnNames(), "=?" ) .addCondition( indexFormulas, "=?" ) .addColumn("1") .toStatementString(); } protected String generateSelectRowByIndexString() { if ( !hasIndex() ) { return null; } return new SimpleSelect(dialect) .setTableName( getTableName() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -