📄 abstractcollectionpersister.java
字号:
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 int filterCount = collection.getFilterMap().size(); filterNames = new String[filterCount]; filterConditions = new String[filterCount]; iter = collection.getFilterMap().entrySet().iterator(); filterCount = 0; while ( iter.hasNext() ) { final Map.Entry entry = (Map.Entry) iter.next(); filterNames[filterCount] = (String) entry.getKey(); filterConditions[filterCount] = Template.renderWhereStringTemplate( (String) entry.getValue(), FilterImpl.MARKER, dialect ); filterConditions[filterCount] = StringHelper.replace( filterConditions[filterCount], ":", ":" + filterNames[filterCount] + "." ); filterCount++; } } 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 CollectionInitializer createSubselectInitializer(SubselectFetch subselect, SessionImplementor session) { return new SubselectCollectionLoader( this, subselect.toSubselectString( getCollectionType().getLHSPropertyName() ), subselect.getResult(), subselect.getQueryParameters(), session.getFactory(), session.getEnabledFilters() ); } 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 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, SessionImplementor session) throws HibernateException, SQLException { Object element = getElementType().nullSafeGet( rs, elementColumnAliases, session, owner ); return element; } public Object readIndex(ResultSet rs, SessionImplementor session) throws HibernateException, SQLException { Object index = getIndexType().nullSafeGet( rs, indexColumnAliases, 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, SessionImplementor session) throws HibernateException, SQLException { Object id = getIdentifierType().nullSafeGet( rs, unquotedIdentifierColumnName, session, null ); if ( id == null ) throw new HibernateException( "null identifier column for collection: " + role ); return id; } public Object readKey(ResultSet rs, SessionImplementor session) throws HibernateException, SQLException { return getKeyType().nullSafeGet( rs, keyColumnAliases, 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, 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; } /** * Generate a list of collection index, key and element columns */ public String selectFragment(String alias) { SelectFragment frag = new SelectFragment() .setSuffix( "" )//always ignore suffix for collection columns .addColumns( alias, keyColumnNames, keyColumnAliases ); for ( int i=0; i<elementColumnIsSettable.length; i++ ) { if ( elementColumnIsSettable[i] ) { frag.addColumn( alias, elementColumnNames[i], elementColumnAliases[i] ); } else { frag.addFormula( alias, elementFormulaTemplates[i], elementColumnAliases[i] ); } } 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] ); } } } if ( hasIdentifier ) frag.addColumn( alias, identifierColumnName, identifierColumnAlias ); return frag.toFragmentString() .substring( 2 ); //strip leading ',' } 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, "could not delete collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ), getSQLDeleteString() ); } } } public void recreate(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { if ( !isInverse && isRowInsertEnabled() ) { if ( log.isDebugEnabled() ) { log.debug( "Inserting collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) ); } try { //create all the new entries Iterator entries = collection.entries(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -