Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGro
}
appendSql( COMMA_SEPARATOR_CHAR );

renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
if ( tableGroupJoin.getPredicate() != null && !tableGroupJoin.getPredicate().isEmpty() ) {
addAdditionalWherePredicate( tableGroupJoin.getPredicate() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
Expand All @@ -85,6 +86,7 @@
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;
import java.util.Set;

import static org.hibernate.type.SqlTypes.BINARY;
import static org.hibernate.type.SqlTypes.BLOB;
Expand Down Expand Up @@ -564,8 +566,9 @@ public LockingSupport getLockingSupport() {
protected LockingClauseStrategy buildLockingClauseStrategy(
PessimisticLockKind lockKind,
RowLockStrategy rowLockStrategy,
LockOptions lockOptions) {
return new DerbyLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions );
LockOptions lockOptions,
Set<NavigablePath> rootPathsForLocking) {
return new DerbyLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions, rootPathsForLocking );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
Expand Down Expand Up @@ -85,6 +86,7 @@
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Set;

import static org.hibernate.type.SqlTypes.BINARY;
import static org.hibernate.type.SqlTypes.BLOB;
Expand Down Expand Up @@ -551,8 +553,9 @@ public boolean supportsCommentOn() {
protected LockingClauseStrategy buildLockingClauseStrategy(
PessimisticLockKind lockKind,
RowLockStrategy rowLockStrategy,
LockOptions lockOptions) {
return new DerbyLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions );
LockOptions lockOptions,
Set<NavigablePath> rootPathsForLocking) {
return new DerbyLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions, rootPathsForLocking );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import org.hibernate.LockOptions;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.RowLockStrategy;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.internal.PessimisticLockKind;
import org.hibernate.sql.ast.internal.StandardLockingClauseStrategy;
import org.hibernate.sql.ast.spi.SqlAppender;

import java.util.Set;

/**
* StandardLockingClauseStrategy subclass, specific for Derby.
*
Expand All @@ -21,8 +24,9 @@ public DerbyLockingClauseStrategy(
Dialect dialect,
PessimisticLockKind lockKind,
RowLockStrategy rowLockStrategy,
LockOptions lockOptions) {
super( dialect, lockKind, rowLockStrategy, lockOptions );
LockOptions lockOptions,
Set<NavigablePath> rootPathsForLocking) {
super( dialect, lockKind, rowLockStrategy, lockOptions, rootPathsForLocking );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGro
// We have to inject the lateral predicate into the sub-query
final Predicate lateralPredicate = this.lateralPredicate;
this.lateralPredicate = predicate;
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
this.lateralPredicate = lateralPredicate;
}
else {
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), predicate, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, predicate, tableGroupJoinCollector );
}
}
else {
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableMutationStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ForUpdateFragment;
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
Expand All @@ -65,6 +66,7 @@
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
import java.util.Set;

import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate;
import static org.hibernate.type.SqlTypes.BIGINT;
Expand Down Expand Up @@ -529,13 +531,14 @@ public LockingSupport getLockingSupport() {
protected LockingClauseStrategy buildLockingClauseStrategy(
PessimisticLockKind lockKind,
RowLockStrategy rowLockStrategy,
LockOptions lockOptions) {
LockOptions lockOptions,
Set<NavigablePath> rootPathsForLocking) {
if ( getVersion().isBefore( 14 ) ) {
return NonLockingClauseStrategy.NON_CLAUSE_STRATEGY;
}
// we'll reuse the StandardLockingClauseStrategy for the collecting
// aspect and just handle the special rendering in the SQL AST translator
return super.buildLockingClauseStrategy( lockKind, rowLockStrategy, lockOptions );
return super.buildLockingClauseStrategy( lockKind, rowLockStrategy, lockOptions, rootPathsForLocking );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.internal.TransactSQLLockingClauseStrategy;
import org.hibernate.sql.ast.spi.LockingClauseStrategy;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.select.QuerySpec;
Expand All @@ -40,7 +41,6 @@
import java.sql.Types;
import java.util.Map;

import static org.hibernate.sql.ast.internal.NonLockingClauseStrategy.NON_CLAUSE_STRATEGY;
import static org.hibernate.type.SqlTypes.BLOB;
import static org.hibernate.type.SqlTypes.BOOLEAN;
import static org.hibernate.type.SqlTypes.CLOB;
Expand Down Expand Up @@ -200,8 +200,7 @@ public boolean qualifyIndexName() {

@Override
public LockingClauseStrategy getLockingClauseStrategy(QuerySpec querySpec, LockOptions lockOptions) {
// T-SQL uses table-based lock hints and thus does not support FOR UPDATE clause
return NON_CLAUSE_STRATEGY;
return new TransactSQLLockingClauseStrategy( lockOptions.getScope(), querySpec.getRootPathsForLocking() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ForUpdateFragment;
import org.hibernate.sql.ast.SqlAstNodeRenderingMode;
import org.hibernate.sql.ast.SqlAstTranslatorFactory;
Expand Down Expand Up @@ -2369,14 +2370,15 @@ public LockingClauseStrategy getLockingClauseStrategy(QuerySpec querySpec, LockO
default -> throw new IllegalStateException( "Should never happen due to checks above" );
}

return buildLockingClauseStrategy( lockKind, rowLockStrategy, lockOptions );
return buildLockingClauseStrategy( lockKind, rowLockStrategy, lockOptions, querySpec.getRootPathsForLocking() );
}

protected LockingClauseStrategy buildLockingClauseStrategy(
PessimisticLockKind lockKind,
RowLockStrategy rowLockStrategy,
LockOptions lockOptions) {
return new StandardLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions );
LockOptions lockOptions,
Set<NavigablePath> rootPathsForLocking) {
return new StandardLockingClauseStrategy( this, lockKind, rowLockStrategy, lockOptions, rootPathsForLocking );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public void lock(
lockOptions.setScope( lockScope );
lockOptions.setTimeOut( timeout );

final var rootQuerySpec = new QuerySpec( true );
final var entityPath = new NavigablePath( entityToLock.getRootPathName() );

final var rootQuerySpec = new QuerySpec( true );
rootQuerySpec.applyRootPathForLocking( entityPath );

final var idMapping = entityToLock.getIdentifierMapping();

// NOTE: there are 2 possible ways to handle the select list for the query...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGro
}
appendSql( COMMA_SEPARATOR_CHAR );

renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
if ( tableGroupJoin.getPredicate() != null && !tableGroupJoin.getPredicate().isEmpty() ) {
addAdditionalWherePredicate( tableGroupJoin.getPredicate() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGro
// We have to inject the lateral predicate into the sub-query
final Predicate lateralPredicate = this.lateralPredicate;
this.lateralPredicate = predicate;
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
this.lateralPredicate = lateralPredicate;
}
else {
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), predicate, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, predicate, tableGroupJoinCollector );
}
}
else {
renderJoinedTableGroup( tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector );
renderJoinedTableGroup( tableGroupJoin, null, tableGroupJoinCollector );
}
}

Expand All @@ -202,7 +202,9 @@ public void renderNamedSetReturningFunction(String functionName, List<? extends
@Override
protected boolean renderNamedTableReference(NamedTableReference tableReference, LockMode lockMode) {
final String tableExpression = tableReference.getTableExpression();
if ( tableReference instanceof UnionTableReference && lockMode != LockMode.NONE && tableExpression.charAt( 0 ) == '(' ) {
if ( tableReference instanceof UnionTableReference
&& lockMode != LockMode.NONE
&& tableExpression.charAt( 0 ) == '(' ) {
// SQL Server requires to push down the lock hint to the actual table names
int searchIndex = 0;
int unionIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public static SelectStatement createSelectBySingleArrayParameter(
final var sqlAstCreationState = builder.createSqlAstCreationState( rootQuerySpec );

final var rootNavigablePath = new NavigablePath( loadable.getRootPathName() );
rootQuerySpec.applyRootPathForLocking( rootNavigablePath );

final var rootTableGroup =
builder.buildRootTableGroup( rootNavigablePath, rootQuerySpec, sqlAstCreationState );

Expand Down Expand Up @@ -464,6 +466,8 @@ private SelectStatement generateSelect() {
final var rootNavigablePath = new NavigablePath( loadable.getRootPathName() );

final var rootQuerySpec = new QuerySpec( true );
rootQuerySpec.applyRootPathForLocking( rootNavigablePath );

final var sqlAstCreationState = createSqlAstCreationState( rootQuerySpec );

final var rootTableGroup = buildRootTableGroup( rootNavigablePath, rootQuerySpec, sqlAstCreationState );
Expand Down Expand Up @@ -980,12 +984,11 @@ private SelectStatement generateSelect(SubselectFetch subselect) {
// - so `loadable` is the owner entity-descriptor and the `partsToSelect` is the collection

assert loadable instanceof PluralAttributeMapping;

final var attributeMapping = (PluralAttributeMapping) loadable;

final var rootQuerySpec = new QuerySpec( true );

final var rootNavigablePath = new NavigablePath( loadable.getRootPathName() );
final var rootQuerySpec = new QuerySpec( true );
rootQuerySpec.applyRootPathForLocking( rootNavigablePath );

// We need to initialize the acronymMap based on subselect.getLoadingSqlAst() to avoid alias collisions
final var tableReferences = AliasCollector.getTableReferences( subselect.getLoadingSqlAst() );
Expand Down
Loading