📄 abstractcollectionpersister.java
字号:
.addCondition( getKeyColumnNames(), "=?" ) .addCondition( getIndexColumnNames(), "=?" ) .addCondition( indexFormulas, "=?" ) .addColumns( getElementColumnNames(), elementColumnAliases ) .addColumns( indexFormulas, indexColumnAliases ) .toStatementString(); } protected String generateDetectRowByElementString() { return new SimpleSelect(dialect) .setTableName( getTableName() ) .addCondition( getKeyColumnNames(), "=?" ) .addCondition( getElementColumnNames(), "=?" ) .addCondition( elementFormulas, "=?" ) .addColumn("1") .toStatementString(); } protected SelectFragment generateSelectFragment(String alias, String columnSuffix) { return new SelectFragment() .setSuffix( columnSuffix ) .addColumns( alias, keyColumnNames, keyColumnAliases ); } 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[] getIndexFormulas() { return indexFormulas; } public String[] getIndexColumnNames(String alias) { return qualify(alias, indexColumnNames, indexFormulaTemplates); } public String[] getElementColumnNames(String alias) { return qualify(alias, elementColumnNames, elementFormulaTemplates); } private static String[] qualify(String alias, String[] columnNames, String[] formulaTemplates) { int span = columnNames.length; String[] result = new String[span]; for (int i=0; i<span; i++) { if ( columnNames[i]==null ) { result[i] = StringHelper.replace( formulaTemplates[i], Template.TEMPLATE, alias ); } else { result[i] = StringHelper.qualify( alias, columnNames[i] ); } } return result; } 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; Expectation expectation = Expectations.appropriateExpectation( getDeleteAllCheckStyle() ); boolean callable = isDeleteAllCallable(); boolean useBatch = expectation.canBeBatched(); String sql = getSQLDeleteString(); if ( useBatch ) { if ( callable ) { st = session.getBatcher().prepareBatchCallableStatement( sql ); } else { st = session.getBatcher().prepareBatchStatement( sql ); } } else { if ( callable ) { st = session.getBatcher().prepareCallableStatement( sql ); } else { st = session.getBatcher().prepareStatement( sql ); } } try { offset+= expectation.prepare( st ); writeKey( st, id, offset, session ); if ( useBatch ) { session.getBatcher().addToBatch( expectation ); } else { expectation.verifyOutcome( st.executeUpdate(), st, -1 ); } } catch ( SQLException sqle ) { if ( useBatch ) { session.getBatcher().abortBatch( sqle ); } throw sqle; } finally { if ( !useBatch ) { session.getBatcher().closeStatement( st ); } } 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); if ( entries.hasNext() ) { collection.preInsert( this ); int i = 0; int count = 0; while ( entries.hasNext() ) { final Object entry = entries.next(); if ( collection.entryExists( entry, i ) ) { int offset = 1; PreparedStatement st = null; Expectation expectation = Expectations.appropriateExpectation( getInsertCheckStyle() ); boolean callable = isInsertCallable(); boolean useBatch = expectation.canBeBatched(); String sql = getSQLInsertRowString(); if ( useBatch ) { if ( callable ) { st = session.getBatcher().prepareBatchCallableStatement( sql ); } else { st = session.getBatcher().prepareBatchStatement( sql ); } } else { if ( callable ) { st = session.getBatcher().prepareCallableStatement( sql ); } else { st = session.getBatcher().prepareStatement( sql ); } } try { offset+= expectation.prepare( st ); //TODO: copy/paste from insertRows() int loc = writeKey( st, id, offset, session ); if ( hasIdentifier ) { loc = writeIdentifier( st, collection.getIdentifier(entry, i), loc, session ); } if ( hasIndex /*&& !indexIsFormula*/ ) { loc = writeIndex( st, collection.getIndex(entry, i, this), loc, session ); } loc = writeElement(st, collection.getElement(entry), loc, session ); if ( useBatch ) { session.getBatcher().addToBatch( expectation ); } else { expectation.verifyOutcome( st.executeUpdate(), st, -1 ); } collection.afterRowInsert( this, entry, i ); count++; } catch ( SQLException sqle ) { if ( useBatch ) { session.getBatcher().abortBatch( sqle ); } throw sqle; } finally { if ( !useBatch ) { session.getBatcher().closeStatement( st ); } } } i++; } if ( log.isDebugEnabled() ) { log.debug( "done inserting collection: " + count + " rows inserted" ); } } else { if ( log.isDebugEnabled() ) { log.debug( "collection was empty" ); } } } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( sqlExceptionConverter, sqle, "could not insert collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ), getSQLInsertRowString() ); } } } protected boolean isRowDeleteEnabled() { return true; } public void deleteRows(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { if ( !isInverse && isRowDeleteEnabled() ) { if ( log.isDebugEnabled() ) { log.debug( "Deleting rows of collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) ); } boolean deleteByIndex = !isOneToMany() && hasIndex && !indexContainsFormula; try { //delete all the deleted entries Iterator deletes = collection.getDeletes( this, !deleteByIndex ); if ( deletes.hasNext() ) { int offset = 1; int count = 0; while ( deletes.hasNext() ) { PreparedStatement st = null; Expectation expectation = Expectations.appropriateExpectation( getDeleteCheckStyle() ); boolean callable = isDeleteCallable(); boolean useBatch = expectation.canBeBatched(); String sql = getSQLDeleteRowString(); if ( useBatch ) { if ( callable ) { st = session.getBatcher().prepareBatchCallableStatement( sql ); } else { st = session.getBatcher().prepareBatchStatement( sql ); } } else { if ( callable ) { st = session.getBatcher().prepareCallableStatement( sql ); } else { st = session.getBatcher().prepareStatement( sql ); } } try { expectation.prepare( st ); Object entry = deletes.next(); int loc = offset; if ( hasIdentifier ) { writeIdentifier( st, entry, loc, session ); } else { loc = writeKey( st, id, loc, session ); if ( deleteByIndex ) { writeIndexToWhere( st, entry, loc, session ); } else { writeElementToWhere( st, entry, loc, session ); } } if ( useBatch ) { session.getBatcher().addToBatch( expectation ); } else { expectation.verifyOutcome( st.executeUpdate(), st, -1 ); } count++; } catch ( SQLException sqle ) { if ( useBatch ) { session.getBatcher().abortBatch( sqle ); } throw sqle; } finally { if ( !useBatch ) { session.getBatcher().closeStatement( st ); } } if ( log.isDebugEnabled() ) { log.debug( "done deleting collection rows: " + count + " deleted" ); } } } else { if ( log.isDebugEnabled() ) { log.debug( "no rows to delete" ); } } } catch ( SQLException sqle ) { throw JDBCExceptionHelper.convert( sqlExceptionConverter, sqle, "could not delete collection rows: " + MessageHelper.collectionInfoString( this, id, getFactory() ), getSQLDeleteRowString() ); } } } protected boolean isRowInsertEnabled() { return true; } public void insertRows(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { if ( !isInverse && isRowInsertEnabled() ) { if ( log.isDebugEnabled() ) { log.debug( "Inserting rows of collection: " + MessageHelper.collectionInfoString( this, id, getFactory() ) ); } try { //insert all the new entries collection.preInsert( this ); Iterator entries = collection.entries( this ); Expectation expectation = Expectations.appropriateExpectation( getInsertCheckStyle() ); boolean callable = isInsertCallable(); boolean useBatch = expectation.canBeBatched(); String sql = getSQLInsertRowString(); int i = 0; int count = 0; while ( entries.hasNext() ) { int offset = 1; Object entry = entries.next(); PreparedStatement st = null; if ( collection.needsInserting( entry, i, elementType ) ) { if ( useBatch ) { if ( st == null ) { if ( callable ) { st = session.getBatcher().prepareBatchCallableStatement( sql ); } else { st = session.getBatcher().prepareBatchStatement( sql ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -