Search in sources :

Example 1 with Update

use of org.hibernate.sql.Update in project hibernate-orm by hibernate.

the class UpdateLockingStrategy method generateLockString.

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();
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Update(org.hibernate.sql.Update)

Example 2 with Update

use of org.hibernate.sql.Update in project hibernate-orm by hibernate.

the class AbstractInlineIdsUpdateHandlerImpl method execute.

@Override
public int execute(SharedSessionContractImplementor session, QueryParameters queryParameters) {
    IdsClauseBuilder values = prepareInlineStatement(session, queryParameters);
    if (!values.getIds().isEmpty()) {
        String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
        String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
        String idSubselect = values.toStatement();
        assignmentParameterSpecifications = new ParameterSpecification[tableNames.length][];
        for (int tableIndex = 0; tableIndex < tableNames.length; tableIndex++) {
            boolean affected = false;
            final List<ParameterSpecification> parameterList = new ArrayList<>();
            Update update = generateUpdate(tableNames[tableIndex], columnNames[tableIndex], idSubselect, "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.put(tableIndex, update.toStatementString());
                assignmentParameterSpecifications[tableIndex] = parameterList.toArray(new ParameterSpecification[parameterList.size()]);
            }
        }
        // Start performing the updates
        for (Map.Entry<Integer, String> updateEntry : updates.entrySet()) {
            int i = updateEntry.getKey();
            String update = updateEntry.getValue();
            if (update == null) {
                continue;
            }
            try {
                try (PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(update, false)) {
                    // jdbc params are 1-based
                    int position = 1;
                    if (assignmentParameterSpecifications[i] != null) {
                        for (int x = 0; x < assignmentParameterSpecifications[i].length; x++) {
                            position += assignmentParameterSpecifications[i][x].bind(ps, queryParameters, session, position);
                        }
                    }
                    session.getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
                }
            } catch (SQLException e) {
                throw convert(e, "error performing bulk update", update);
            }
        }
    }
    return values.getIds().size();
}
Also used : ParameterSpecification(org.hibernate.param.ParameterSpecification) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Update(org.hibernate.sql.Update) AssignmentSpecification(org.hibernate.hql.internal.ast.tree.AssignmentSpecification) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with Update

use of org.hibernate.sql.Update in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateVersionIncrementUpdateString.

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(getIdentifierColumnNames());
    update.setVersionColumnName(getVersionColumnName());
    return update.toStatementString();
}
Also used : Update(org.hibernate.sql.Update)

Example 4 with Update

use of org.hibernate.sql.Update in project hibernate-orm by hibernate.

the class OneToManyPersister method generateDeleteRowString.

/**
 * 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();
}
Also used : Update(org.hibernate.sql.Update)

Example 5 with Update

use of org.hibernate.sql.Update in project hibernate-orm by hibernate.

the class OneToManyPersister method generateUpdateRowString.

/**
 * 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();
}
Also used : Update(org.hibernate.sql.Update)

Aggregations

Update (org.hibernate.sql.Update)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 PreparedStatement (java.sql.PreparedStatement)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BeforeTransactionCompletionProcess (org.hibernate.action.spi.BeforeTransactionCompletionProcess)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 RevisionType (org.hibernate.envers.RevisionType)1 AssignmentSpecification (org.hibernate.hql.internal.ast.tree.AssignmentSpecification)1 ReturningWork (org.hibernate.jdbc.ReturningWork)1 ParameterSpecification (org.hibernate.param.ParameterSpecification)1 Queryable (org.hibernate.persister.entity.Queryable)1 UnionSubclassEntityPersister (org.hibernate.persister.entity.UnionSubclassEntityPersister)1 CollectionType (org.hibernate.type.CollectionType)1 ComponentType (org.hibernate.type.ComponentType)1 MapType (org.hibernate.type.MapType)1