⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcollectionpersister.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		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);
	}
	
	public void writeElement(PreparedStatement st, Object elt, boolean writeOrder, SessionImplementor session)
	throws HibernateException, SQLException {
		getElementType().nullSafeSet(
			st, 
			elt, 
			1+( writeOrder?0:keyColumnNames.length+(hasIndex?indexColumnNames.length:0)+(hasIdentifier?1:0) ), 
			session
		);
	}
	
	public void writeIndex(PreparedStatement st, Object idx, boolean writeOrder, SessionImplementor session)
	throws HibernateException, SQLException {
		getIndexType().nullSafeSet(st, idx, 1+keyColumnNames.length + (writeOrder?elementColumnNames.length:0), session);
	}
	
	public void writeIdentifier(PreparedStatement st, Object idx, boolean writeOrder, SessionImplementor session)
	throws HibernateException, SQLException {
		getIdentifierType().nullSafeSet(st, idx, 1+(writeOrder?elementColumnNames.length:keyColumnNames.length), session);
	}
	
	private void writeRowSelect(PreparedStatement st, Object idx, SessionImplementor session)
	throws HibernateException, SQLException {
		rowSelectType.nullSafeSet(st, idx, 1+(hasIdentifier?0:keyColumnNames.length), session);
	}
	
	public void writeKey(PreparedStatement st, Serializable id, boolean writeOrder, SessionImplementor session)
	throws HibernateException, SQLException {
		if (id==null) throw new NullPointerException("null key for collection: " + role);  //an assertion
		getKeyType().nullSafeSet(st, id, 1+(writeOrder?elementColumnNames.length:0), session);
	}
	
	public boolean isPrimitiveArray() {
		return primitiveArray;
	}
	
	public boolean isArray() {
		return array;
	}
	
	/**
	 * Generate a list of collection index and element columns
	 */
	public String selectClauseFragment(String alias) {
		SelectFragment frag = new SelectFragment()
			.setSuffix(StringHelper.EMPTY_STRING)
			.addColumns(alias, elementColumnNames, elementColumnAliases)
			.addColumns(alias, keyColumnNames, keyColumnAliases);
		if (hasIndex) frag.addColumns(alias, indexColumnNames, indexColumnAliases);
		if (hasIdentifier) frag.addColumn(alias, identifierColumnName, identifierColumnAlias);
		return frag.toFragmentString()
			.substring(2); //strip leading ','
	}
	
	/**
	 * Generate a list of collection index, key and element columns
	 */
	public String multiselectClauseFragment(String alias) {
		SelectFragment frag = new SelectFragment()
			.setSuffix(StringHelper.EMPTY_STRING)
			.addColumns(alias, elementColumnNames, elementColumnAliases)
			.addColumns(alias, keyColumnNames, keyColumnAliases);
		if (hasIndex) frag.addColumns(alias, indexColumnNames, indexColumnAliases);
		if (hasIdentifier) frag.addColumn(alias, identifierColumnName, identifierColumnAlias);
		return frag.toFragmentString()
			.substring(2); //strip leading ','
	}
	
	/*private String sqlSelectString() {
		//we no longer have Jon Lipsky's patch to allow a Map from id's to objects
		SimpleSelect select = new SimpleSelect()
			.setTableName(qualifiedTableName)
			.addColumns(elementColumnNames);
		if (hasIndex) select.addColumns(indexColumnNames);
		select.addCondition( keyColumnNames, "=?" );
		if (hasWhere) select.addWhereToken( " and " + sqlWhereString );
		if (hasOrder) select.setOrderBy(sqlOrderByString);
		return select.toStatementString();
	}*/
	
	public String[] getIndexColumnNames() {
		return indexColumnNames;
	}
	
	public String[] getElementColumnNames() {
		return elementColumnNames;
	}
	
	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 final void remove(Serializable id, SessionImplementor session) throws HibernateException {
		
		if ( !isInverse ) {
			
			if ( log.isDebugEnabled() ) log.debug( "Deleting collection: " + MessageHelper.infoString(this, id) );
			
			// Remove all the old entries
			
			try {
				PreparedStatement st = session.getBatcher().prepareBatchStatement( getSQLDeleteString() );
				
				try {
					writeKey(st, id, false, 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 new JDBCException("could not delete collection: " + MessageHelper.infoString(this, id), sqle );
			}
			
		}
		
	}
	
	public final void recreate(PersistentCollection collection, Serializable id, SessionImplementor session)
	throws HibernateException {
		
		if (!isInverse) {
			
			if ( log.isDebugEnabled() ) log.debug( "Inserting collection: " + MessageHelper.infoString(this, id) );
			
			try {
				//create all the new entries
				Iterator entries = collection.entries();
				if ( entries.hasNext() ) {
					try {
						collection.preInsert(this);
						int i=0;
						int count=0;
						while ( entries.hasNext() ) {
							Object entry = entries.next();
							if ( collection.entryExists(entry, i) ) {
								PreparedStatement st = session.getBatcher().prepareBatchStatement( getSQLInsertRowString() );
								writeKey(st, id, false, session);
								collection.writeTo(st, this, entry, i, false);
								session.getBatcher().addToBatch(1);
								collection.afterRowInsert(this, entry, i);
								count++;
							}
							i++;
						}
						if ( log.isDebugEnabled() ) log.debug("done inserting collection: " + count + " rows inserted");
					}
					catch (SQLException sqle) {
						session.getBatcher().abortBatch(sqle);
						throw sqle;
					}
					
				}
				else {
					if ( log.isDebugEnabled() ) log.debug("collection was empty");
				}
			}
			catch (SQLException sqle) {
				throw new JDBCException("could not insert collection: " + MessageHelper.infoString(this, id), sqle );
			}
		}
	}
	
	public final void deleteRows(PersistentCollection collection, Serializable id, SessionImplementor session)
	throws HibernateException {
		
		if (!isInverse) {
			
			if ( log.isDebugEnabled() ) log.debug( "Deleting rows of collection: " + MessageHelper.infoString(this, id) );
			
			try {
				//delete all the deleted entries
				Iterator entries = collection.getDeletes(elementType);
				if ( entries.hasNext() ) {
					int count=0;
					PreparedStatement st = session.getBatcher().prepareBatchStatement( getSQLDeleteRowString() );
					
					try {
						while ( entries.hasNext() ) {
							if (!hasIdentifier) writeKey(st, id, false, session );
							writeRowSelect( st, entries.next(), session );
							session.getBatcher().addToBatch(-1);
							count++;
						}
					}
					catch (SQLException sqle) {
						session.getBatcher().abortBatch(sqle);
						throw sqle;
					}
					
					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 new JDBCException("could not delete collection rows: " + MessageHelper.infoString(this, id), sqle );
			}
		}
	}
	
	public final void insertRows(PersistentCollection collection, Serializable id, SessionImplementor session)
	throws HibernateException {
		
		if (!isInverse) {
			
			if ( log.isDebugEnabled() ) log.debug( "Inserting rows of collection: " + MessageHelper.infoString(this, id) );
			
			try {
				//insert all the new entries
				Iterator entries = collection.entries();
				try {
					collection.preInsert(this);
					int i=0;
					int count=0;
					while ( entries.hasNext() ) {
						Object entry = entries.next();
						if ( collection.needsInserting(entry, i, elementType) ) {
							PreparedStatement st = session.getBatcher().prepareBatchStatement( getSQLInsertRowString() );
							writeKey(st, id, false, session);
							collection.writeTo( st, this, entry, i, false );
							session.getBatcher().addToBatch(1);
							collection.afterRowInsert(this, entry, i);
							count++;
						}
						i++;
					}
					if ( log.isDebugEnabled() ) log.debug("done inserting rows: " + count + " inserted");
				}
				catch (SQLException sqle) {
					session.getBatcher().abortBatch(sqle);
					throw sqle;
				}
			}
			catch (SQLException sqle) {
				throw new JDBCException("could not insert collection rows: " + MessageHelper.infoString(this, id), sqle );
			}
			
		}
	}
	
	
	public String getRole() {
		return role;
	}
	
	public Class getOwnerClass() {
		return ownerClass;
	}
	
	public IdentifierGenerator getIdentifierGenerator() {
		return identifierGenerator;
	}
	
	public Type getIdentifierType() {
		return identifierType;
	}
	
	public boolean hasOrphanDelete() {
		return hasOrphanDelete;
	}
	
	private void checkColumnDuplication(java.util.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 collection: " +
				role +
				" column: " + 
				col.getName()
			);
		}
	}

	public Type toType(String propertyName) throws QueryException {
		if ( "index".equals(propertyName) ) return indexType;
		return elementPropertyMapping.toType(propertyName);
	}

	public String[] toColumns(String alias, String propertyName)
		throws QueryException {
		
		if ( "index".equals(propertyName) ) {
			if ( isManyToMany() ) throw new QueryException("index() function not supported for many-to-many association");
			return StringHelper.qualify(alias, indexColumnNames);
		}
		return elementPropertyMapping.toColumns(alias, propertyName);
	}

	public Type getType() {
		return elementPropertyMapping.getType(); //==elementType ??
	}

	public String fromJoinFragment(String alias, boolean innerJoin, boolean includeSubclasses) {
		return StringHelper.EMPTY_STRING;
	}

	public String[] getJoinKeyColumnNames() {
		return getKeyColumnNames();
	}

	public String getName() {
		return getRole();
	}

	public Loadable getElementPersister() {
		if (elementPersister==null) throw new AssertionFailure("not an association");
		return elementPersister;
	}

	public String whereJoinFragment(String alias, boolean innerJoin, boolean includeSubclasses) {
		return StringHelper.EMPTY_STRING;
	}

	public boolean isCollection() {
		return true;
	}

	public Serializable getCollectionSpace() {
		return getTableName();
	}

	protected abstract String generateDeleteString();
	protected abstract String generateDeleteRowString();
	protected abstract String generateUpdateRowString();
	protected abstract String generateInsertRowString();

	public final void updateRows(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException {
		
		if (!isInverse) {
			
			if ( log.isDebugEnabled() ) log.debug("Updating rows of collection: " + role + "#" + id);
			
			//update all the modified entries
			int count = doUpdateRows(id, collection, session);
			
			if ( log.isDebugEnabled() ) log.debug("done updating rows: " + count + " updated");
		}
	}
	
	protected abstract int doUpdateRows(Serializable key, PersistentCollection collection, SessionImplementor session) throws HibernateException;

	public CollectionMetadata getCollectionMetadata() {
		return this;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -