Skip to content

Commit 5a91673

Browse files
committed
HHH-5274 - HQL-Insert with Select and Sub-Select fails
1 parent fe60239 commit 5a91673

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/FromElementType.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ String[] toColumns(String tableAlias, String path, boolean inSelect, boolean for
457457
// table name as the column qualification
458458
// 2) otherwise (not correlated), use the given alias
459459
if ( isCorrelation() ) {
460-
if ( isMultiTable() ) {
460+
if ( isMultiTable() || isInsertQuery() ) {
461461
return propertyMapping.toColumns( tableAlias, path );
462462
}
463463
return propertyMapping.toColumns( extractTableName(), path );
@@ -498,6 +498,10 @@ private String extractTableName() {
498498
return fromElement.getQueryable().getTableName();
499499
}
500500

501+
private boolean isInsertQuery() {
502+
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.INSERT;
503+
}
504+
501505
private boolean isManipulationQuery() {
502506
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.UPDATE
503507
|| fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.DELETE;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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.test.hql;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
13+
import org.hibernate.QueryException;
14+
import org.hibernate.Session;
15+
import org.hibernate.Transaction;
16+
17+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18+
import org.junit.Test;
19+
20+
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
21+
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
22+
import static org.junit.Assert.fail;
23+
24+
/**
25+
* @author bjoern.moritz
26+
*/
27+
public class InsertWithSubSelectTest extends BaseCoreFunctionalTestCase {
28+
29+
@Override
30+
protected Class<?>[] getAnnotatedClasses() {
31+
return new Class[] {
32+
A.class,
33+
B.class,
34+
C.class
35+
};
36+
}
37+
38+
@Test
39+
public void testInsert() {
40+
doInHibernate( this::sessionFactory, session -> {
41+
session.createQuery(
42+
"insert into C (id) " +
43+
"select a.id from A a " +
44+
"where exists (" +
45+
" select 1 " +
46+
" from B b " +
47+
" where b.id = a.id" +
48+
")"
49+
)
50+
.executeUpdate();
51+
} );
52+
}
53+
54+
@Test
55+
public void testSelect() {
56+
doInHibernate( this::sessionFactory, session -> {
57+
session.createQuery(
58+
"select a.id " +
59+
"from A a " +
60+
"where exists (" +
61+
" select 1 " +
62+
" from B b " +
63+
" where b.id = a.id" +
64+
")"
65+
)
66+
.getResultList();
67+
} );
68+
}
69+
70+
@Entity(name = "A")
71+
public static class A {
72+
73+
@Id
74+
@GeneratedValue
75+
private Integer id;
76+
77+
public Integer getId() {
78+
return id;
79+
}
80+
81+
public void setId(Integer id) {
82+
this.id = id;
83+
}
84+
}
85+
86+
@Entity(name = "B")
87+
public static class B {
88+
89+
@Id
90+
@GeneratedValue
91+
private Integer id;
92+
93+
public Integer getId() {
94+
return id;
95+
}
96+
97+
public void setId(Integer id) {
98+
this.id = id;
99+
}
100+
}
101+
102+
@Entity(name = "C")
103+
public static class C {
104+
105+
@Id
106+
@GeneratedValue
107+
private Integer id;
108+
109+
public Integer getId() {
110+
return id;
111+
}
112+
113+
public void setId(Integer id) {
114+
this.id = id;
115+
}
116+
}
117+
118+
}

0 commit comments

Comments
 (0)