Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-19314 StackOverflowException when using onConflict with createCriteriaInsertValues and createCriteriaInsertSelect #9953

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -71,6 +71,10 @@ protected List<SqmPath<?>> copyInsertionTargetPaths(SqmCopyContext context) {
}
}

void setConflictClause(SqmConflictClause<T> conflictClause) {
this.conflictClause = conflictClause;
}

protected void verifyInsertTypesMatch(
List<SqmPath<?>> insertionTargetPaths,
List<? extends SqmTypedNode<?>> expressions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private SqmConflictClause(
this.insertStatement = insertStatement;
this.excludedRoot = excludedRoot;
this.constraintName = constraintName;
this.constraintPaths = Collections.unmodifiableList( constraintPaths );
this.constraintPaths = constraintPaths == null ? null : Collections.unmodifiableList( constraintPaths );
this.updateAction = updateAction;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ public SqmConflictClause<T> copy(SqmCopyContext context) {
insertStatement.copy( context ),
excludedRoot.copy( context ),
constraintName,
copyOf( constraintPaths, context ),
constraintPaths == null ? null : copyOf( constraintPaths, context ),
updateAction == null ? null : updateAction.copy( context )
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,26 @@ public SqmInsertSelectStatement<T> copy(SqmCopyContext context) {
if ( existing != null ) {
return existing;
}
return context.registerCopy(
this,
new SqmInsertSelectStatement<>(
nodeBuilder(),
context.getQuerySource() == null ? getQuerySource() : context.getQuerySource(),
copyParameters( context ),
copyCteStatements( context ),
getTarget().copy( context ),
copyInsertionTargetPaths( context ),
getConflictClause() == null ? null : getConflictClause().copy( context ),
selectQueryPart.copy( context )
)
final SqmInsertSelectStatement<T> sqmInsertSelectStatementCopy = new SqmInsertSelectStatement<>(
nodeBuilder(),
context.getQuerySource() == null ? getQuerySource() : context.getQuerySource(),
copyParameters( context ),
copyCteStatements( context ),
getTarget().copy( context ),
null,
null,
selectQueryPart.copy( context )
);

context.registerCopy( this, sqmInsertSelectStatementCopy );

sqmInsertSelectStatementCopy.setInsertionTargetPaths( copyInsertionTargetPaths( context ) );

if ( getConflictClause() != null ) {
sqmInsertSelectStatementCopy.setConflictClause( getConflictClause().copy( context ) );
}

return sqmInsertSelectStatementCopy;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public SqmInsertValuesStatement<T> copy(SqmCopyContext context) {
valuesList.add( sqmValues.copy( context ) );
}
}
return context.registerCopy(

final SqmInsertValuesStatement<T> sqmInsertValuesStatementCopy = context.registerCopy(
this,
new SqmInsertValuesStatement<>(
nodeBuilder(),
Expand All @@ -95,10 +96,16 @@ public SqmInsertValuesStatement<T> copy(SqmCopyContext context) {
copyCteStatements( context ),
getTarget().copy( context ),
copyInsertionTargetPaths( context ),
getConflictClause() == null ? null : getConflictClause().copy( context ),
null,
valuesList
)
);

if ( getConflictClause() != null ) {
sqmInsertValuesStatementCopy.setConflictClause( getConflictClause().copy( context ) );
}

return sqmInsertValuesStatementCopy;
}

public SqmInsertValuesStatement<T> copyWithoutValues(SqmCopyContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
import org.hibernate.sql.exec.internal.AbstractJdbcParameter;
import org.hibernate.sql.exec.internal.JdbcOperationQueryInsertImpl;
import org.hibernate.sql.exec.internal.JdbcParameterBindingImpl;
import org.hibernate.sql.exec.internal.JdbcParameterImpl;
import org.hibernate.sql.exec.internal.JdbcParametersImpl;
import org.hibernate.sql.exec.internal.SqlTypedMappingJdbcParameter;
import org.hibernate.sql.exec.spi.ExecutionContext;
Expand Down Expand Up @@ -6742,9 +6743,17 @@ private int getSortSelectionIndex(QuerySpec querySpec, SortSpecification sortSpe

private boolean isFetchFirstRowOnly(QueryPart queryPart) {
return queryPart.getFetchClauseType() == FetchClauseType.ROWS_ONLY
&& queryPart.getFetchClauseExpression() instanceof QueryLiteral<?>
&& Integer.valueOf( 1 )
.equals( ( (QueryLiteral<?>) queryPart.getFetchClauseExpression() ).getLiteralValue() );
&& queryPart.getFetchClauseExpression() != null
&& Integer.valueOf( 1 ).equals( getLiteralValue( queryPart.getFetchClauseExpression() ) );
}

private boolean isParameterValueEqualToOne(SqmParameterInterpretation sqmParameterInterpretation) {
if ( sqmParameterInterpretation.getResolvedExpression() instanceof JdbcParameterImpl jdbcParameter ) {
assert jdbcParameterBindings != null;
final JdbcParameterBinding binding = jdbcParameterBindings.getBinding( jdbcParameter );
return binding != null && Integer.valueOf( 1 ).equals( binding.getBindValue() );
}
return false;
}

private SelectStatement stripToSelectClause(SelectStatement statement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query.hql;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Tuple;
import org.hibernate.cfg.QuerySettings;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaInsertSelect;
import org.hibernate.query.criteria.JpaCriteriaInsertValues;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;


@DomainModel(
annotatedClasses = {
InsertConflictWithCriteriaCopyTreeEnabledTests.TestEntity.class,
InsertConflictWithCriteriaCopyTreeEnabledTests.AnotherTestEntity.class,
}
)
@ServiceRegistry(
settings = {@Setting(name = QuerySettings.CRITERIA_COPY_TREE, value = "true")}
)
@SessionFactory
@JiraKey("HHH-19314")
public class InsertConflictWithCriteriaCopyTreeEnabledTests {

@Test
void createCriteriaInsertValuesTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
HibernateCriteriaBuilder cb = session.getCriteriaBuilder();

JpaCriteriaInsertValues<TestEntity> insertIntoItem = cb
.createCriteriaInsertValues( TestEntity.class );
insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) );
insertIntoItem.values( cb.values( cb.value( 1L ) ) );
insertIntoItem.onConflict().onConflictDoNothing();

session.createMutationQuery( insertIntoItem ).executeUpdate();
}
);
}

@Test
void createCriteriaInsertSelectTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
HibernateCriteriaBuilder cb = session.getCriteriaBuilder();

JpaCriteriaInsertSelect<TestEntity> insertIntoItem = cb
.createCriteriaInsertSelect( TestEntity.class );
insertIntoItem.setInsertionTargetPaths( insertIntoItem.getTarget().get( "id" ) );

JpaCriteriaQuery<Tuple> cq = cb.createQuery( Tuple.class );
cq.select( cb.tuple( cb.literal( 1 ) ) );
cq.fetch( 1 );
insertIntoItem.select( cq );
insertIntoItem.onConflict().onConflictDoNothing();

session.createMutationQuery( insertIntoItem ).executeUpdate();
}
);
}

@Entity(name = "TestEntity")
public static class TestEntity {
@Id
private Long id;

private String name;

}

@Entity(name = "AnotherTestEntity")
public static class AnotherTestEntity {
@Id
private Long id;

private String name;

}
}
Loading