Thanks to visit codestin.com
Credit goes to github.com

Skip to content

HHH-18813: Fix of generated Insert-Query in CteUpdateHandler #10173

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

Merged
merged 1 commit into from
May 20, 2025
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 @@ -153,8 +153,8 @@ protected void addDmlCtes(
tableExpression,
true
);
final List<Assignment> assignmentList = assignmentsByTable.get( updatingTableReference );
if ( assignmentList == null ) {
final List<Assignment> assignmentsForInsert = assignmentsByTable.get( updatingTableReference );
if ( assignmentsForInsert == null ) {
continue;
}
final String insertCteTableName = getInsertCteTableName( tableExpression );
Expand Down Expand Up @@ -229,7 +229,7 @@ protected void addDmlCtes(
// Collect the target column references from the key expressions
final List<ColumnReference> targetColumnReferences = new ArrayList<>( existsKeyColumns );
// And transform assignments to target column references and selections
for ( Assignment assignment : assignments ) {
for ( Assignment assignment : assignmentsForInsert ) {
targetColumnReferences.addAll( assignment.getAssignable().getColumnReferences() );
querySpec.getSelectClause().addSqlSelection(
new SqlSelectionImpl(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.secondarytable;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.SecondaryTable;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;


/**
* Test for a Bugfix described in HHH-18813.
* The CteUpdateHandler generated an Insert-Query,
* which contained columns that do not exist in the target table.
*
* @author Peter Bambazek
*/
@JiraKey(value = "HHH-18813")
@DomainModel(
annotatedClasses = {HHH18813Test.SecondaryTableEntitySub.class, HHH18813Test.SecondaryTableEntityBase.class})
@SessionFactory
class HHH18813Test {

@Test
void hhh18813Test(SessionFactoryScope scope) {

// prepare
scope.inTransaction( session -> {
SecondaryTableEntitySub entitySub = new SecondaryTableEntitySub();
entitySub.setB( 111L );
entitySub.setC( 222L );
session.persist( entitySub );
} );

// asset before
scope.inTransaction( session -> {
SecondaryTableEntitySub entitySub = session.createQuery(
"select s from SecondaryTableEntitySub s",
SecondaryTableEntitySub.class ).getSingleResult();
assertNotNull( entitySub );
assertEquals( 111L, entitySub.getB() );
assertEquals( 222L, entitySub.getC() );
} );

// update
scope.inTransaction( session -> {
session.createMutationQuery( "update SecondaryTableEntitySub e set e.b=:b, e.c=:c" )
.setParameter( "b", 333L )
.setParameter( "c", 444L )
.executeUpdate();
} );

// asset after
scope.inTransaction( session -> {
SecondaryTableEntitySub entitySub = session.createQuery( "select s from SecondaryTableEntitySub s",
SecondaryTableEntitySub.class ).getSingleResult();
assertNotNull( entitySub );
assertEquals( 333L, entitySub.getB() );
assertEquals( 444L, entitySub.getC() );
} );
}

@Entity(name = "SecondaryTableEntitySub")
@Inheritance(strategy = InheritanceType.JOINED)
@SecondaryTable(name = "test")
public static class SecondaryTableEntitySub extends SecondaryTableEntityBase {

@Column
private Long b;

@Column(table = "test")
private Long c;

public Long getB() {
return b;
}

public void setB(Long b) {
this.b = b;
}

public Long getC() {
return c;
}

public void setC(Long c) {
this.c = c;
}
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public static class SecondaryTableEntityBase {

@Id
@GeneratedValue
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
}