Skip to content

Commit db233c6

Browse files
peter1123581321yrodiere
authored andcommitted
HHH-18813: Fix of generated Insert-Query in CteUpdateHandler
1 parent bb931c7 commit db233c6

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/cte/CteUpdateHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ protected void addDmlCtes(
157157
tableExpression,
158158
true
159159
);
160-
final List<Assignment> assignmentList = assignmentsByTable.get( updatingTableReference );
161-
if ( assignmentList == null ) {
160+
final List<Assignment> assignmentsForInsert = assignmentsByTable.get( updatingTableReference );
161+
if ( assignmentsForInsert == null ) {
162162
continue;
163163
}
164164
final String insertCteTableName = getInsertCteTableName( tableExpression );
@@ -233,7 +233,7 @@ protected void addDmlCtes(
233233
// Collect the target column references from the key expressions
234234
final List<ColumnReference> targetColumnReferences = new ArrayList<>( existsKeyColumns );
235235
// And transform assignments to target column references and selections
236-
for ( Assignment assignment : assignments ) {
236+
for ( Assignment assignment : assignmentsForInsert ) {
237237
targetColumnReferences.addAll( assignment.getAssignable().getColumnReferences() );
238238
querySpec.getSelectClause().addSqlSelection(
239239
new SqlSelectionImpl(
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.orm.test.secondarytable;
8+
9+
import jakarta.persistence.Column;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.Inheritance;
14+
import jakarta.persistence.InheritanceType;
15+
import jakarta.persistence.SecondaryTable;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.JiraKey;
18+
import org.hibernate.testing.orm.junit.SessionFactory;
19+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
25+
26+
/**
27+
* Test for a Bugfix described in HHH-18813.
28+
* The CteUpdateHandler generated an Insert-Query,
29+
* which contained columns that do not exist in the target table.
30+
*
31+
* @author Peter Bambazek
32+
*/
33+
@JiraKey(value = "HHH-18813")
34+
@DomainModel(
35+
annotatedClasses = {HHH18813Test.SecondaryTableEntitySub.class, HHH18813Test.SecondaryTableEntityBase.class})
36+
@SessionFactory
37+
class HHH18813Test {
38+
39+
@Test
40+
void hhh18813Test(SessionFactoryScope scope) {
41+
42+
// prepare
43+
scope.inTransaction( session -> {
44+
SecondaryTableEntitySub entitySub = new SecondaryTableEntitySub();
45+
entitySub.setB( 111L );
46+
entitySub.setC( 222L );
47+
session.persist( entitySub );
48+
} );
49+
50+
// asset before
51+
scope.inTransaction( session -> {
52+
SecondaryTableEntitySub entitySub = session.createQuery(
53+
"select s from SecondaryTableEntitySub s",
54+
SecondaryTableEntitySub.class ).getSingleResult();
55+
assertNotNull( entitySub );
56+
assertEquals( 111L, entitySub.getB() );
57+
assertEquals( 222L, entitySub.getC() );
58+
} );
59+
60+
// update
61+
scope.inTransaction( session -> {
62+
session.createMutationQuery( "update SecondaryTableEntitySub e set e.b=:b, e.c=:c" )
63+
.setParameter( "b", 333L )
64+
.setParameter( "c", 444L )
65+
.executeUpdate();
66+
} );
67+
68+
// asset after
69+
scope.inTransaction( session -> {
70+
SecondaryTableEntitySub entitySub = session.createQuery( "select s from SecondaryTableEntitySub s",
71+
SecondaryTableEntitySub.class ).getSingleResult();
72+
assertNotNull( entitySub );
73+
assertEquals( 333L, entitySub.getB() );
74+
assertEquals( 444L, entitySub.getC() );
75+
} );
76+
}
77+
78+
@Entity(name = "SecondaryTableEntitySub")
79+
@Inheritance(strategy = InheritanceType.JOINED)
80+
@SecondaryTable(name = "test")
81+
public static class SecondaryTableEntitySub extends SecondaryTableEntityBase {
82+
83+
@Column
84+
private Long b;
85+
86+
@Column(table = "test")
87+
private Long c;
88+
89+
public Long getB() {
90+
return b;
91+
}
92+
93+
public void setB(Long b) {
94+
this.b = b;
95+
}
96+
97+
public Long getC() {
98+
return c;
99+
}
100+
101+
public void setC(Long c) {
102+
this.c = c;
103+
}
104+
}
105+
106+
@Entity
107+
@Inheritance(strategy = InheritanceType.JOINED)
108+
public static class SecondaryTableEntityBase {
109+
110+
@Id
111+
@GeneratedValue
112+
private Long id;
113+
114+
public Long getId() {
115+
return id;
116+
}
117+
118+
public void setId(Long id) {
119+
this.id = id;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)