Java Code Examples for org.hibernate.sql.Update

The following examples show how to use org.hibernate.sql.Update. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lams   Source File: OneToManyPersister.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate the SQL UPDATE that updates all the foreign keys to null
 */
@Override
protected String generateDeleteString() {
	final Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames, "null" )
			.addPrimaryKeyColumns( keyColumnNames );

	if ( hasIndex && !indexContainsFormula ) {
		for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
			if ( indexColumnIsSettable[i] ) {
				update.addColumn( indexColumnNames[i], "null" );
			}
		}
	}

	if ( hasWhere ) {
		update.setWhere( sqlWhereString );
	}

	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( "delete one-to-many " + getRole() );
	}

	return update.toStatementString();
}
 
Example 2
Source Project: lams   Source File: OneToManyPersister.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate the SQL UPDATE that updates a foreign key to a value
 */
@Override
protected String generateInsertRowString() {
	final Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames );

	if ( hasIndex && !indexContainsFormula ) {
		for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
			if ( indexColumnIsSettable[i] ) {
				update.addColumn( indexColumnNames[i] );
			}
		}
	}

	//identifier collections not supported for 1-to-many

	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( "create one-to-many row " + getRole() );
	}

	return update.addPrimaryKeyColumns( elementColumnNames, elementColumnWriters )
			.toStatementString();
}
 
Example 3
Source Project: lams   Source File: OneToManyPersister.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate the SQL UPDATE that inserts a collection index
 */
@Override
protected String generateUpdateRowString() {
	final Update update = new Update( getDialect() ).setTableName( qualifiedTableName );
	update.addPrimaryKeyColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
	if ( hasIdentifier ) {
		update.addPrimaryKeyColumns( new String[] {identifierColumnName} );
	}
	if ( hasIndex && !indexContainsFormula ) {
		for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
			if ( indexColumnIsSettable[i] ) {
				update.addColumn( indexColumnNames[i] );
			}
		}
	}

	return update.toStatementString();
}
 
Example 4
Source Project: lams   Source File: OneToManyPersister.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate the SQL UPDATE that updates a particular row's foreign
 * key to null
 */
@Override
protected String generateDeleteRowString() {
	final Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames, "null" );

	if ( hasIndex && !indexContainsFormula ) {
		for ( int i = 0 ; i < indexColumnNames.length ; i++ ) {
			if ( indexColumnIsSettable[i] ) {
				update.addColumn( indexColumnNames[i], "null" );
			}
		}
	}

	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( "delete one-to-many row " + getRole() );
	}

	//use a combination of foreign key columns and pk columns, since
	//the ordering of removal and addition is not guaranteed when
	//a child moves from one parent to another
	String[] rowSelectColumnNames = ArrayHelper.join( keyColumnNames, elementColumnNames );
	return update.addPrimaryKeyColumns( rowSelectColumnNames )
			.toStatementString();
}
 
Example 5
/**
 * Generate the SQL UPDATE that updates a row
 */
protected String generateUpdateRowString() {
	
	Update update = new Update( getDialect() )
		.setTableName( qualifiedTableName );
	
	//if ( !elementIsFormula ) {
		update.addColumns( elementColumnNames, elementColumnIsSettable );
	//}
	
	if ( hasIdentifier ) {
		update.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
	}
	else if ( hasIndex && !indexContainsFormula ) {
		update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
	}
	else {
		update.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
	}
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "update collection row " + getRole() );
	}
	
	return update.toStatementString();
}
 
Example 6
/**
 * Generate the SQL UPDATE that updates all the foreign keys to null
 */
protected String generateDeleteString() {
	
	Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames, "null" )
			.setPrimaryKeyColumnNames( keyColumnNames );
	
	if ( hasIndex && !indexContainsFormula ) update.addColumns( indexColumnNames, "null" );
	
	if ( hasWhere ) update.setWhere( sqlWhereString );
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "delete one-to-many " + getRole() );
	}
	
	return update.toStatementString();
}
 
Example 7
/**
 * Generate the SQL UPDATE that updates a foreign key to a value
 */
protected String generateInsertRowString() {
	
	Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames );
	
	if ( hasIndex && !indexContainsFormula ) update.addColumns( indexColumnNames );
	
	//identifier collections not supported for 1-to-many
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "create one-to-many row " + getRole() );
	}
	
	return update.setPrimaryKeyColumnNames( elementColumnNames )
			.toStatementString();
}
 
Example 8
/**
 * Generate the SQL UPDATE that updates a particular row's foreign
 * key to null
 */
protected String generateDeleteRowString() {
	
	Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName )
			.addColumns( keyColumnNames, "null" );
	
	if ( hasIndex && !indexContainsFormula ) update.addColumns( indexColumnNames, "null" );
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "delete one-to-many row " + getRole() );
	}
	
	//use a combination of foreign key columns and pk columns, since
	//the ordering of removal and addition is not guaranteed when
	//a child moves from one parent to another
	String[] rowSelectColumnNames = ArrayHelper.join(keyColumnNames, elementColumnNames);
	return update.setPrimaryKeyColumnNames( rowSelectColumnNames )
			.toStatementString();
}
 
Example 9
default String generateUpdateLockString(LockOptions lockOptions) {
	final SessionFactoryImplementor factory = getFactory();
	Dialect dialect = factory.getJdbcServices().getDialect();
	final Update update = new Update(dialect);
	update.setTableName( getRootTableName() );
	update.addPrimaryKeyColumns( getRootTableIdentifierColumnNames() );
	update.setVersionColumnName( getVersionColumnName() );
	update.addColumn( getVersionColumnName() );
	if ( factory.getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( lockOptions.getLockMode() + " lock " + getEntityName() );
	}
	return update.toStatementString();
}
 
Example 10
Source Project: lams   Source File: AbstractEntityPersister.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String generateVersionIncrementUpdateString() {
	Update update = new Update( getFactory().getDialect() );
	update.setTableName( getTableName( 0 ) );
	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( "forced version increment" );
	}
	update.addColumn( getVersionColumnName() );
	update.addPrimaryKeyColumns( rootTableKeyColumnNames );
	update.setVersionColumnName( getVersionColumnName() );
	return update.toStatementString();
}
 
Example 11
/**
 * Generate the SQL UPDATE that updates a row
 */
@Override
protected String generateUpdateRowString() {
	final Update update = new Update( getDialect() )
			.setTableName( qualifiedTableName );

	//if ( !elementIsFormula ) {
	update.addColumns( elementColumnNames, elementColumnIsSettable, elementColumnWriters );
	//}

	if ( hasIdentifier ) {
		update.addPrimaryKeyColumns( new String[] {identifierColumnName} );
	}
	else if ( hasIndex && !indexContainsFormula ) {
		update.addPrimaryKeyColumns( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
	}
	else {
		update.addPrimaryKeyColumns( keyColumnNames );
		update.addPrimaryKeyColumns( elementColumnNames, elementColumnIsInPrimaryKey, elementColumnWriters );
	}

	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( "update collection row " + getRole() );
	}

	return update.toStatementString();
}
 
Example 12
Source Project: lams   Source File: UpdateLockingStrategy.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected String generateLockString() {
	final SessionFactoryImplementor factory = lockable.getFactory();
	final Update update = new Update( factory.getDialect() );
	update.setTableName( lockable.getRootTableName() );
	update.addPrimaryKeyColumns( lockable.getRootTableIdentifierColumnNames() );
	update.setVersionColumnName( lockable.getVersionColumnName() );
	update.addColumn( lockable.getVersionColumnName() );
	if ( factory.getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( lockMode + " lock " + lockable.getEntityName() );
	}
	return update.toStatementString();
}
 
Example 13
protected String generateLockString() {
	final SessionFactoryImplementor factory = lockable.getFactory();
	final Update update = new Update( factory.getDialect() );
	update.setTableName( lockable.getRootTableName() );
	update.addPrimaryKeyColumns( lockable.getRootTableIdentifierColumnNames() );
	update.setVersionColumnName( lockable.getVersionColumnName() );
	update.addColumn( lockable.getVersionColumnName() );
	if ( factory.getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( lockMode + " lock " + lockable.getEntityName() );
	}
	return update.toStatementString();
}
 
Example 14
protected String generateLockString() {
	final SessionFactoryImplementor factory = lockable.getFactory();
	final Update update = new Update( factory.getDialect() );
	update.setTableName( lockable.getRootTableName() );
	update.addPrimaryKeyColumns( lockable.getRootTableIdentifierColumnNames() );
	update.setVersionColumnName( lockable.getVersionColumnName() );
	update.addColumn( lockable.getVersionColumnName() );
	if ( factory.getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( lockMode + " lock " + lockable.getEntityName() );
	}
	return update.toStatementString();
}
 
Example 15
public CteValuesListUpdateHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker,
		String catalog,
		String schema) {
	super( factory, walker, catalog, schema );

	String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
	String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
	String idSubselect = generateIdSubselect( getTargetedQueryable() );

	updates = new String[tableNames.length];
	assignmentParameterSpecifications = new ParameterSpecification[tableNames.length][];
	for ( int tableIndex = 0; tableIndex < tableNames.length; tableIndex++ ) {
		boolean affected = false;
		final List<ParameterSpecification> parameterList = new ArrayList<>();
		final Update update = new Update( factory.getServiceRegistry().getService( JdbcServices.class ).getDialect() )
				.setTableName( tableNames[tableIndex] )
				.setWhere( "(" + String.join( ", ", (CharSequence[]) columnNames[tableIndex] ) + ") in (" + idSubselect + ")" );
		if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
			update.setComment( "bulk update" );
		}
		final List<AssignmentSpecification> assignmentSpecifications = walker.getAssignmentSpecifications();
		for ( AssignmentSpecification assignmentSpecification : assignmentSpecifications ) {
			if ( assignmentSpecification.affectsTable( tableNames[tableIndex] ) ) {
				affected = true;
				update.appendAssignmentFragment( assignmentSpecification.getSqlAssignmentFragment() );
				if ( assignmentSpecification.getParameters() != null ) {
					Collections.addAll( parameterList, assignmentSpecification.getParameters() );
				}
			}
		}
		if ( affected ) {
			updates[tableIndex] = update.toStatementString();
			assignmentParameterSpecifications[tableIndex] = parameterList.toArray( new ParameterSpecification[parameterList.size()] );
		}
	}
}
 
Example 16
@Override
protected Update generateUpdate(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Update update = new Update( factory().getServiceRegistry().getService( JdbcServices.class ).getDialect() )
			.setTableName( tableName )
			.setWhere( idSubselect );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( comment );
	}
	return update;
}
 
Example 17
protected Update generateUpdate(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Update update = new Update( factory().getServiceRegistry().getService( JdbcServices.class ).getDialect() )
			.setTableName( tableName )
			.setWhere( "(" + String.join( ", ", (CharSequence[]) columnNames ) + ") in (" + idSubselect + ")" );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( comment );
	}
	return update;
}
 
Example 18
private String generateVersionIncrementUpdateString() {
	Update update = new Update( getFactory().getDialect() );
	update.setTableName( getTableName( 0 ) );
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "forced version increment" );
	}
	update.addColumn( getVersionColumnName() );
	update.setPrimaryKeyColumnNames( getIdentifierColumnNames() );
	update.setVersionColumnName( getVersionColumnName() );
	return update.toStatementString();
}
 
Example 19
protected String generateLockString() {
	SessionFactoryImplementor factory = lockable.getFactory();
	Update update = new Update( factory.getDialect() );
	update.setTableName( lockable.getRootTableName() );
	update.setPrimaryKeyColumnNames( lockable.getRootTableIdentifierColumnNames() );
	update.setVersionColumnName( lockable.getVersionColumnName() );
	update.addColumn( lockable.getVersionColumnName() );
	if ( factory.getSettings().isCommentsEnabled() ) {
		update.setComment( lockMode + " lock " + lockable.getEntityName() );
	}
	return update.toStatementString();
}
 
Example 20
@SuppressWarnings("unchecked")
public TableBasedUpdateHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker,
		IdTableInfo idTableInfo) {
	super( factory, walker );

	final Dialect dialect = factory.getJdbcServices().getJdbcEnvironment().getDialect();
	final UpdateStatement updateStatement = (UpdateStatement) walker.getAST();
	final FromElement fromElement = updateStatement.getFromClause().getFromElement();

	this.targetedPersister = fromElement.getQueryable();

	final String bulkTargetAlias = fromElement.getTableAlias();

	final ProcessedWhereClause processedWhereClause = processWhereClause( updateStatement.getWhereClause() );
	this.idSelectParameterSpecifications = processedWhereClause.getIdSelectParameterSpecifications();
	this.idInsertSelect = generateIdInsertSelect( bulkTargetAlias, idTableInfo, processedWhereClause );
	log.tracev( "Generated ID-INSERT-SELECT SQL (multi-table update) : {0}", idInsertSelect );

	String[] tableNames = targetedPersister.getConstraintOrderedTableNameClosure();
	String[][] columnNames = targetedPersister.getContraintOrderedTableKeyColumnClosure();
	String idSubselect = generateIdSubselect( targetedPersister, idTableInfo );

	updates = new String[tableNames.length];
	assignmentParameterSpecifications = new ParameterSpecification[tableNames.length][];
	for ( int tableIndex = 0; tableIndex < tableNames.length; tableIndex++ ) {
		boolean affected = false;
		final List<ParameterSpecification> parameterList = new ArrayList<>();
		final Update update = new Update( dialect )
				.setTableName( tableNames[tableIndex] )
				.setWhere( "(" + String.join( ", ", columnNames[tableIndex] ) + ") IN (" + idSubselect + ")" );
		if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
			update.setComment( "bulk update" );
		}
		final List<AssignmentSpecification> assignmentSpecifications = walker.getAssignmentSpecifications();
		for ( AssignmentSpecification assignmentSpecification : assignmentSpecifications ) {
			if ( assignmentSpecification.affectsTable( tableNames[tableIndex] ) ) {
				affected = true;
				update.appendAssignmentFragment( assignmentSpecification.getSqlAssignmentFragment() );
				if ( assignmentSpecification.getParameters() != null ) {
					Collections.addAll( parameterList, assignmentSpecification.getParameters() );
				}
			}
		}
		if ( affected ) {
			updates[tableIndex] = update.toStatementString();
			assignmentParameterSpecifications[tableIndex] = parameterList.toArray( new ParameterSpecification[parameterList.size()] );
		}
	}
}
 
Example 21
/**
 * Generate the SQL that updates a row by id (and version)
 */
protected String generateUpdateString(final boolean[] includeProperty,
									  final int j,
									  final Object[] oldFields,
									  final boolean useRowId) {

	Update update = new Update( getFactory().getDialect() ).setTableName( getTableName( j ) );

	// select the correct row by either pk or rowid
	if ( useRowId ) {
		update.setPrimaryKeyColumnNames( new String[]{rowIdName} ); //TODO: eventually, rowIdName[j]
	}
	else {
		update.setPrimaryKeyColumnNames( getKeyColumns( j ) );
	}

	boolean hasColumns = false;
	for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
		if ( includeProperty[i] && isPropertyOfTable( i, j ) ) {
			// this is a property of the table, which we are updating
			update.addColumns( getPropertyColumnNames(i), propertyColumnUpdateable[i] );
			hasColumns = hasColumns || getPropertyColumnSpan( i ) > 0;
		}
	}

	if ( j == 0 && isVersioned() && entityMetamodel.getOptimisticLockMode() == Versioning.OPTIMISTIC_LOCK_VERSION ) {
		// this is the root (versioned) table, and we are using version-based
		// optimistic locking;  if we are not updating the version, also don't
		// check it (unless this is a "generated" version column)!
		if ( checkVersion( includeProperty ) ) {
			update.setVersionColumnName( getVersionColumnName() );
			hasColumns = true;
		}
	}
	else if ( entityMetamodel.getOptimisticLockMode() > Versioning.OPTIMISTIC_LOCK_VERSION && oldFields != null ) {
		// we are using "all" or "dirty" property-based optimistic locking

		boolean[] includeInWhere = entityMetamodel.getOptimisticLockMode() == Versioning.OPTIMISTIC_LOCK_ALL ?
				getPropertyUpdateability() : //optimistic-lock="all", include all updatable properties
				includeProperty; //optimistic-lock="dirty", include all properties we are updating this time

		boolean[] versionability = getPropertyVersionability();
		Type[] types = getPropertyTypes();
		for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
			boolean include = includeInWhere[i] &&
					isPropertyOfTable( i, j ) &&
					versionability[i];
			if ( include ) {
				// this property belongs to the table, and it is not specifically
				// excluded from optimistic locking by optimistic-lock="false"
				String[] propertyColumnNames = getPropertyColumnNames( i );
				boolean[] propertyNullness = types[i].toColumnNullness( oldFields[i], getFactory() );
				for ( int k=0; k<propertyNullness.length; k++ ) {
					if ( propertyNullness[k] ) {
						update.addWhereColumn( propertyColumnNames[k] );
					}
					else {
						update.addWhereColumn( propertyColumnNames[k], " is null" );
					}
				}
			}
		}

	}

	if ( getFactory().getSettings().isCommentsEnabled() ) {
		update.setComment( "update " + getEntityName() );
	}

	return hasColumns ? update.toStatementString() : null;
}
 
Example 22
public MultiTableUpdateExecutor(HqlSqlWalker walker) {
	super( walker, log );

	if ( !walker.getSessionFactoryHelper().getFactory().getDialect().supportsTemporaryTables() ) {
		throw new HibernateException( "cannot perform multi-table updates using dialect not supporting temp tables" );
	}

	UpdateStatement updateStatement = ( UpdateStatement ) walker.getAST();
	FromElement fromElement = updateStatement.getFromClause().getFromElement();
	String bulkTargetAlias = fromElement.getTableAlias();
	this.persister = fromElement.getQueryable();

	this.idInsertSelect = generateIdInsertSelect( persister, bulkTargetAlias, updateStatement.getWhereClause() );
	log.trace( "Generated ID-INSERT-SELECT SQL (multi-table update) : " +  idInsertSelect );

	String[] tableNames = persister.getConstraintOrderedTableNameClosure();
	String[][] columnNames = persister.getContraintOrderedTableKeyColumnClosure();

	String idSubselect = generateIdSubselect( persister );
	List assignmentSpecifications = walker.getAssignmentSpecifications();

	updates = new String[tableNames.length];
	hqlParameters = new ParameterSpecification[tableNames.length][];
	for ( int tableIndex = 0; tableIndex < tableNames.length; tableIndex++ ) {
		boolean affected = false;
		List parameterList = new ArrayList();
		Update update = new Update( getFactory().getDialect() )
				.setTableName( tableNames[tableIndex] )
				.setWhere( "(" + StringHelper.join( ", ", columnNames[tableIndex] ) + ") IN (" + idSubselect + ")" );
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			update.setComment( "bulk update" );
		}
		final Iterator itr = assignmentSpecifications.iterator();
		while ( itr.hasNext() ) {
			final AssignmentSpecification specification = ( AssignmentSpecification ) itr.next();
			if ( specification.affectsTable( tableNames[tableIndex] ) ) {
				affected = true;
				update.appendAssignmentFragment( specification.getSqlAssignmentFragment() );
				if ( specification.getParameters() != null ) {
					for ( int paramIndex = 0; paramIndex < specification.getParameters().length; paramIndex++ ) {
						parameterList.add( specification.getParameters()[paramIndex] );
					}
				}
			}
		}
		if ( affected ) {
			updates[tableIndex] = update.toStatementString();
			hqlParameters[tableIndex] = ( ParameterSpecification[] ) parameterList.toArray( new ParameterSpecification[0] );
		}
	}
}