HHH-5274 - HQL-Insert with Select and Sub-Select fails

This commit is contained in:
Vlad Mihalcea 2017-11-15 11:27:17 +02:00
parent fe60239301
commit 5a91673ef2
2 changed files with 123 additions and 1 deletions

View File

@ -457,7 +457,7 @@ class FromElementType {
// table name as the column qualification
// 2) otherwise (not correlated), use the given alias
if ( isCorrelation() ) {
if ( isMultiTable() ) {
if ( isMultiTable() || isInsertQuery() ) {
return propertyMapping.toColumns( tableAlias, path );
}
return propertyMapping.toColumns( extractTableName(), path );
@ -498,6 +498,10 @@ class FromElementType {
return fromElement.getQueryable().getTableName();
}
private boolean isInsertQuery() {
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.INSERT;
}
private boolean isManipulationQuery() {
return fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.UPDATE
|| fromElement.getWalker().getStatementType() == HqlSqlTokenTypes.DELETE;

View File

@ -0,0 +1,118 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.hql;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.fail;
/**
* @author bjoern.moritz
*/
public class InsertWithSubSelectTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
A.class,
B.class,
C.class
};
}
@Test
public void testInsert() {
doInHibernate( this::sessionFactory, session -> {
session.createQuery(
"insert into C (id) " +
"select a.id from A a " +
"where exists (" +
" select 1 " +
" from B b " +
" where b.id = a.id" +
")"
)
.executeUpdate();
} );
}
@Test
public void testSelect() {
doInHibernate( this::sessionFactory, session -> {
session.createQuery(
"select a.id " +
"from A a " +
"where exists (" +
" select 1 " +
" from B b " +
" where b.id = a.id" +
")"
)
.getResultList();
} );
}
@Entity(name = "A")
public static class A {
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity(name = "B")
public static class B {
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity(name = "C")
public static class C {
@Id
@GeneratedValue
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}