Java Code Examples for org.hibernate.sql.Delete

The following examples show how to use org.hibernate.sql.Delete. 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
/**
 * Generate the SQL DELETE that deletes all rows
 */
@Override
protected String generateDeleteString() {
	final Delete delete = new Delete()
			.setTableName( qualifiedTableName )
			.addPrimaryKeyColumns( keyColumnNames );

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

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

	return delete.toStatementString();
}
 
Example 2
/**
 * Generate the SQL DELETE that deletes a particular row
 */
@Override
protected String generateDeleteRowString() {
	final Delete delete = new Delete().setTableName( qualifiedTableName );

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

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

	return delete.toStatementString();
}
 
Example 3
/**
 * Generate the SQL DELETE that deletes a particular row
 */
protected String generateDeleteRowString() {
	
	Delete delete = new Delete()
		.setTableName( qualifiedTableName );
	
	if ( hasIdentifier ) {
		delete.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } );
	}
	else if ( hasIndex && !indexContainsFormula ) {
		delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) );
	}
	else {
		delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) );
	}
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		delete.setComment( "delete collection row " + getRole() );
	}
	
	return delete.toStatementString();
}
 
Example 4
default String[] generateSQLDeleteStrings(Object[] loadedState) {
	int span = delegate().getTableSpan();
	String[] deleteStrings = new String[span];
	for ( int j = span - 1; j >= 0; j-- ) {
		Delete delete = new Delete()
				.setTableName( delegate().getTableName( j ) )
				.addPrimaryKeyColumns( delegate().getKeyColumns( j ) );
		if ( delegate().getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
			delete.setComment( "delete " + delegate().getEntityName() + " [" + j + "]" );
		}

		boolean[] versionability = delegate().getPropertyVersionability();
		Type[] types = delegate().getPropertyTypes();
		for (int i = 0; i < delegate().getEntityMetamodel().getPropertySpan(); i++ ) {
			if ( delegate().isPropertyOfTable( i, j ) && versionability[i] ) {
				// this property belongs to the table and it is not specifically
				// excluded from optimistic locking by optimistic-lock="false"
				String[] propertyColumnNames = delegate().getPropertyColumnNames( i );
				boolean[] propertyNullness = types[i].toColumnNullness( loadedState[i], delegate().getFactory() );
				for ( int k = 0; k < propertyNullness.length; k++ ) {
					if ( propertyNullness[k] ) {
						delete.addWhereFragment( propertyColumnNames[k] + " = $" + ( k + 1 ) );
					}
					else {
						delete.addWhereFragment( propertyColumnNames[k] + " is null" );
					}
				}
			}
		}
		deleteStrings[j] = delete.toStatementString();
	}
	return deleteStrings;
}
 
Example 5
Source Project: lams   Source File: AbstractEntityPersister.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate the SQL that deletes a row by id (and version)
 */
protected String generateDeleteString(int j) {
	final Delete delete = new Delete()
			.setTableName( getTableName( j ) )
			.addPrimaryKeyColumns( getKeyColumns( j ) );
	if ( j == 0 ) {
		delete.setVersionColumnName( getVersionColumnName() );
	}
	if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
		delete.setComment( "delete " + getEntityName() );
	}
	return delete.toStatementString();
}
 
Example 6
Source Project: lams   Source File: AbstractEntityPersister.java    License: GNU General Public License v2.0 5 votes vote down vote up
private String[] generateSQLDeletStrings(Object[] loadedState) {
	int span = getTableSpan();
	String[] deleteStrings = new String[span];
	for ( int j = span - 1; j >= 0; j-- ) {
		Delete delete = new Delete()
				.setTableName( getTableName( j ) )
				.addPrimaryKeyColumns( getKeyColumns( j ) );
		if ( getFactory().getSessionFactoryOptions().isCommentsEnabled() ) {
			delete.setComment( "delete " + getEntityName() + " [" + j + "]" );
		}

		boolean[] versionability = getPropertyVersionability();
		Type[] types = getPropertyTypes();
		for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
			if ( isPropertyOfTable( i, j ) && versionability[i] ) {
				// 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( loadedState[i], getFactory() );
				for ( int k = 0; k < propertyNullness.length; k++ ) {
					if ( propertyNullness[k] ) {
						delete.addWhereFragment( propertyColumnNames[k] + " = ?" );
					}
					else {
						delete.addWhereFragment( propertyColumnNames[k] + " is null" );
					}
				}
			}
		}
		deleteStrings[j] = delete.toStatementString();
	}
	return deleteStrings;
}
 
Example 7
private String generateDelete(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Delete delete = new Delete().setTableName( tableName ).setWhere(
			"(" + String.join( ", ", (CharSequence[]) columnNames ) + ") in ("
					+ idSubselect + ")" );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		delete.setComment( comment );
	}
	return delete.toStatementString();
}
 
Example 8
private String generateDelete(String tableName, String[] columnNames, String idSubselect, String comment) {
	final Delete delete = new Delete()
			.setTableName( tableName )
			.setWhere( "(" + String.join( ", ", columnNames ) + ") IN (" + idSubselect + ")" );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		delete.setComment( comment );
	}
	return delete.toStatementString();
}
 
Example 9
protected Delete generateDelete(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Delete delete = new Delete().setTableName( tableName ).setWhere(
			"(" + String.join( ", ", (CharSequence[]) columnNames ) + ") in ("
					+ idSubselect + ")" );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		delete.setComment( comment );
	}
	return delete;
}
 
Example 10
protected Delete generateDelete(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Delete delete = new Delete()
			.setTableName( tableName )
			.setWhere(idSubselect);
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		delete.setComment( comment );
	}
	return delete;
}
 
Example 11
/**
 * Generate the SQL that deletes a row by id (and version)
 */
protected String generateDeleteString(int j) {
	Delete delete = new Delete()
			.setTableName( getTableName( j ) )
			.setPrimaryKeyColumnNames( getKeyColumns( j ) );
	if ( j == 0 ) {
		delete.setVersionColumnName( getVersionColumnName() );
	}
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		delete.setComment( "delete " + getEntityName() );
	}
	return delete.toStatementString();
}
 
Example 12
private String[] generateSQLDeletStrings(Object[] loadedState) {
	int span = getTableSpan();
	String[] deleteStrings = new String[span];
	for ( int j = span - 1; j >= 0; j-- ) {
		Delete delete = new Delete()
				.setTableName( getTableName( j ) )
				.setPrimaryKeyColumnNames( getKeyColumns( j ) );
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			delete.setComment( "delete " + getEntityName() + " [" + j + "]" );
		}

		boolean[] versionability = getPropertyVersionability();
		Type[] types = getPropertyTypes();
		for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
			if ( isPropertyOfTable( i, j ) && versionability[i] ) {
				// 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( loadedState[i], getFactory() );
				for ( int k = 0; k < propertyNullness.length; k++ ) {
					if ( propertyNullness[k] ) {
						delete.addWhereFragment( propertyColumnNames[k] + " = ?" );
					}
					else {
						delete.addWhereFragment( propertyColumnNames[k] + " is null" );
					}
				}
			}
		}
		deleteStrings[j] = delete.toStatementString();
	}
	return deleteStrings;
}
 
Example 13
/**
 * Generate the SQL DELETE that deletes all rows
 */
protected String generateDeleteString() {
	
	Delete delete = new Delete()
			.setTableName( qualifiedTableName )
			.setPrimaryKeyColumnNames( keyColumnNames );
	
	if ( hasWhere ) delete.setWhere( sqlWhereString );
	
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		delete.setComment( "delete collection " + getRole() );
	}
	
	return delete.toStatementString();
}
 
Example 14
public MultiTableDeleteExecutor(HqlSqlWalker walker) {
	super( walker, log );

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

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

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

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

	deletes = new String[tableNames.length];
	for ( int i = tableNames.length - 1; i >= 0; i-- ) {
		// TODO : an optimization here would be to consider cascade deletes and not gen those delete statements;
		//      the difficulty is the ordering of the tables here vs the cascade attributes on the persisters ->
		//          the table info gotten here should really be self-contained (i.e., a class representation
		//          defining all the needed attributes), then we could then get an array of those
		final Delete delete = new Delete()
				.setTableName( tableNames[i] )
				.setWhere( "(" + StringHelper.join( ", ", columnNames[i] ) + ") IN (" + idSubselect + ")" );
		if ( getFactory().getSettings().isCommentsEnabled() ) {
			delete.setComment( "bulk delete" );
		}

		deletes[i] = delete.toStatementString();
	}
}
 
Example 15
Source Project: lams   Source File: DeleteExecutor.java    License: GNU General Public License v2.0 4 votes vote down vote up
public DeleteExecutor(HqlSqlWalker walker, Queryable persister) {
	super( walker, persister );
	
	final SessionFactoryImplementor factory = walker.getSessionFactoryHelper().getFactory();
	final Dialect dialect = factory.getJdbcServices().getJdbcEnvironment().getDialect();
	
	try {
		final DeleteStatement deleteStatement = (DeleteStatement) walker.getAST();
		
		final String idSubselectWhere;
		if ( deleteStatement.hasWhereClause() ) {
			final AST whereClause = deleteStatement.getWhereClause();
			final SqlGenerator gen = new SqlGenerator( factory );
			gen.whereClause( whereClause );
			parameterSpecifications = gen.getCollectedParameters();
			idSubselectWhere = gen.getSQL().length() > 7 ? gen.getSQL() : "";
		}
		else {
			parameterSpecifications = new ArrayList<>();
			idSubselectWhere = "";
		}
		
		// If many-to-many, delete the FK row in the collection table.
		for ( Type type : persister.getPropertyTypes() ) {
			if ( type.isCollectionType() ) {
				final CollectionType cType = (CollectionType) type;
				final AbstractCollectionPersister cPersister = (AbstractCollectionPersister) factory.getMetamodel().collectionPersister( cType.getRole() );
				if ( cPersister.isManyToMany() ) {
					if ( persister.getIdentifierColumnNames().length > 1
							&& !dialect.supportsTuplesInSubqueries() ) {
						LOG.warn(
								"This dialect is unable to cascade the delete into the many-to-many join table" +
								" when the entity has multiple primary keys.  Either properly setup cascading on" +
								" the constraints or manually clear the associations prior to deleting the entities."
						);
					}
					else {
						final String idSubselect = "(select "
								+ String.join( ", ", persister.getIdentifierColumnNames() ) + " from "
								+ persister.getTableName() + idSubselectWhere + ")";
						final String where = "(" + String.join( ", ", cPersister.getKeyColumnNames() )
								+ ") in " + idSubselect;
						final Delete delete = new Delete().setTableName( cPersister.getTableName() ).setWhere( where );
						if ( factory.getSessionFactoryOptions().isCommentsEnabled() ) {
							delete.setComment( "delete FKs in join table" );
						}
						deletes.add( delete.toStatementString() );
					}
				}
			}
		}
	}
	catch (RecognitionException e) {
		throw new HibernateException( "Unable to delete the FKs in the join table!", e );
	}
}