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 @@ -72,6 +72,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 @@ -53,7 +53,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 @@ -156,7 +156,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 @@ -82,19 +82,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 @@ -87,7 +87,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 @@ -96,10 +97,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 @@ -66,6 +66,8 @@ public class AnonymousTupleTableGroupProducer implements TableGroupProducer, Map
private final JavaType<?> javaTypeDescriptor;
private final Map<String, ModelPart> modelParts;
private final Set<String> compatibleTableExpressions;
private final SqlTypedMapping[] sqlTypedMappings;
private final int jdbcTypeCount;

public AnonymousTupleTableGroupProducer(
AnonymousTupleType<?> tupleType,
Expand All @@ -75,6 +77,7 @@ public AnonymousTupleTableGroupProducer(
this.aliasStem = aliasStem;
this.javaTypeDescriptor = tupleType.getExpressibleJavaType();
final Set<String> compatibleTableExpressions = new HashSet<>();
this.sqlTypedMappings = sqlTypedMappings;
// The empty table expression is the default for derived model parts
compatibleTableExpressions.add( "" );

Expand Down Expand Up @@ -127,6 +130,7 @@ else if ( sqlTypedMappings[selectionIndex] instanceof SelectableMapping selectab
}
this.modelParts = modelParts;
this.compatibleTableExpressions = compatibleTableExpressions;
jdbcTypeCount = selectionIndex;
}

private ModelPart getModelPart(TableGroup tableGroup) {
Expand Down Expand Up @@ -401,11 +405,16 @@ public <X, Y> int forEachDisassembledJdbcValue(

@Override
public JdbcMapping getJdbcMapping(int index) {
throw new UnsupportedOperationException( "Not yet implemented" );
return sqlTypedMappings[index].getJdbcMapping();
}

@Override
public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
throw new UnsupportedOperationException( "Not yet implemented" );
}

@Override
public int getJdbcTypeCount() {
return jdbcTypeCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6738,8 +6738,8 @@ private int getSortSelectionIndex(QuerySpec querySpec, SortSpecification sortSpe

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

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;

}
}