10000 HHH-18813: Fix of generated Insert-Query in CteUpdateHandler · hibernate/hibernate-orm@2754d54 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2754d54

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

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.secondarytable;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.Inheritance;
12+
import jakarta.persistence.InheritanceType;
13+
import jakarta.persistence.SecondaryTable;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
24+
/**
25+
* Test for a Bugfix described in HHH-18813.
26+
* The CteUpdateHandler generated an Insert-Query,
27+
* which contained columns that do not exist in the target table.
28+
*
29+
* @author Peter Bambazek
30+
*/
31+
@JiraKey(value = "HHH-18813")
32+
@DomainModel(
33+
annotatedClasses = {HHH18813Test.SecondaryTableEntitySub.class, HHH18813Test.SecondaryTableEntityBase.class})
34+
@SessionFactory
35+
class HHH18813Test {
36+
37+
@Test
38+
void hhh18813Test(SessionFactoryScope scope) {
39+
40+
// prepare
41+
scope.inTransaction( session -> {
42+
SecondaryTableEntitySub entitySub = new SecondaryTableEntitySub();
43+
entitySub.setB( 111L );
44+
entitySub.setC( 222L );
45+
session.persist( entitySub );
46+
} );
47+
48+
// asset before
49+
scope.inTransaction( session -> {
50+
SecondaryTableEntitySub entitySub = session.createQuery(
51+
"select s from SecondaryTableEntitySub s",
52+
SecondaryTableEntitySub.class ).getSingleResult();
53+
assertNotNull( entitySub );
54+
assertEquals( 111L, entitySub.getB() );
55+
assertEquals( 222L, entitySub.getC() );
56+
} );
57+
58+
// update
59+
scope.inTransaction( session -> {
60+
session.createMutationQuery( "update SecondaryTableEntitySub e set e.b=:b, e.c=:c" )
61+
.setParameter( "b", 333L )
62+
.setParameter( "c", 444L )
63+
.executeUpdate();
64+
} );
65+
66+
// asset after
67+
scope.inTransaction( session -> {
68+
SecondaryTableEntitySub entitySub = session.createQuery( "select s from SecondaryTableEntitySub s",
69+
SecondaryTableEntitySub.class ).getSingleResult();
70+
assertNotNull( entitySub );
71+
assertEquals( 333L, entitySub.getB() );
72+
assertEquals( 444L, entitySub.getC() );
73+
} );
74+
}
75+
76+
@Entity(name = "SecondaryTableEntitySub")
77+
@Inheritance(strategy = InheritanceType.JOINED)
78+
@SecondaryTable(name = "test")
79+
public static class SecondaryTableEntitySub extends SecondaryTableEntityBase {
80+
81+
@Column
82+
private Long b;
83+
84+
@Column(table = "test")
85+
private Long c;
86+
87+
public Long getB() {
88+
return b;
89+
}
90+
91+
public void setB(Long b) {
92+
this.b = b;
93+
}
94+
95+
public Long getC() {
96+
return c;
97+
}
98+
99+
public void setC(Long c) {
100+
this.c = c;
101+
}
102+
}
103+
104+
@Entity
105+
@Inheritance(strategy = InheritanceType.JOINED)
106+
public static class SecondaryTableEntityBase {
107+
108+
@Id
109+
@GeneratedValue
110+
private Long id;
111+
112+
public Long getId() {
113+
return id;
114+
}
115+
116+
public void setId(Long id) {
117+
this.id = id;
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)
0