Skip to content
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 @@ -170,7 +170,8 @@ private void validateQueryGroupFetchStructure(List<? extends SqmTypedNode<?>> ty
for ( int j = 0; j < firstSelectionSize; j++ ) {
final SqmTypedNode<?> firstSqmSelection = typedNodes.get( j );
final JavaType<?> firstJavaType = firstSqmSelection.getNodeJavaType();
if ( firstJavaType != selections.get( j ).getNodeJavaType() ) {
final JavaType<?> nodeJavaType = selections.get( j ).getNodeJavaType();
if ( nodeJavaType != null && firstJavaType != null && firstJavaType != nodeJavaType ) {
throw new SemanticException(
"Select items of the same index must have the same java type across all query parts"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.results.ResultSetMapping;
import org.hibernate.query.results.ResultSetMappingImpl;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducerProvider;
import org.hibernate.type.descriptor.jdbc.NullJdbcType;

import java.util.List;

/**
* Standard JdbcValuesMappingProducerProvider implementation
Expand All @@ -28,10 +34,20 @@ public class JdbcValuesMappingProducerProviderStandard implements JdbcValuesMapp
public JdbcValuesMappingProducer buildMappingProducer(
SelectStatement sqlAst,
SessionFactoryImplementor sessionFactory) {
return new JdbcValuesMappingProducerStandard(
sqlAst.getQuerySpec().getSelectClause().getSqlSelections(),
sqlAst.getDomainResultDescriptors()
);
return new JdbcValuesMappingProducerStandard( getSelections( sqlAst ), sqlAst.getDomainResultDescriptors() );
}

private static List<SqlSelection> getSelections(SelectStatement selectStatement) {
if ( selectStatement.getQueryPart() instanceof QueryGroup ) {
final QueryGroup queryGroup = (QueryGroup) selectStatement.getQueryPart();
for ( QueryPart queryPart : queryGroup.getQueryParts() ) {
if ( !(queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections()
.get( 0 ).getExpressionType().getSingleJdbcMapping().getJdbcType() instanceof NullJdbcType) ) {
return queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections();
}
}
}
return selectStatement.getQuerySpec().getSelectClause().getSqlSelections();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query.hql;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Tuple;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DomainModel(
annotatedClasses = {
UnionAllSelectNullTest.TestEntity.class,
UnionAllSelectNullTest.AnotherTestEntity.class
}
)
@SessionFactory
@JiraKey("HHH-18720")
class UnionAllSelectNullTest {

@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.persist( new TestEntity( 1L, "a" ) );
session.persist( new AnotherTestEntity( 2L, "b" ) );
}
);
}

@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete TestEntity" ).executeUpdate();
session.createQuery( "delete AnotherTestEntity" ).executeUpdate();
}
);
}

@Test
void testSelect(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<Tuple> resultList = session.createQuery(
"SELECT te.id as id from TestEntity te"
+ " union all SELECT null as id from AnotherTestEntity ate"
, Tuple.class )
.getResultList();
assertThat( resultList.size() ).isEqualTo( 2 );
assertResultIsCorrect( resultList, 1L, null );
}
);
}

@Test
void testSelect2(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<Tuple> resultList = session.createQuery(
"SELECT null as id from TestEntity te"
+ " union all SELECT ate.id as id from AnotherTestEntity ate"
, Tuple.class )
.getResultList();
assertThat( resultList.size() ).isEqualTo( 2 );
assertResultIsCorrect( resultList, null, 2L );
}
);
}

@Test
void testSelect3(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<Tuple> resultList = session.createQuery(
"SELECT te.id as id from TestEntity te"
+ " union all SELECT ate.id as id from AnotherTestEntity ate"
, Tuple.class )
.getResultList();
assertThat( resultList.size() ).isEqualTo( 2 );
assertResultIsCorrect( resultList, 1L, 2L );
}
);
}

private static void assertResultIsCorrect(List<Tuple> resultList, Long id1, Long id2) {
Set<Long> ids = new HashSet<>( 2 );
ids.add( (Long) resultList.get( 0 ).get( "id" ) );
ids.add( (Long) resultList.get( 1 ).get( "id" ) );
assertThat( ids.contains( id1 ) ).as( "Result does not contain expected value:" + id1 ).isTrue();
assertThat( ids.contains( id2 ) ).as( "Result does not contain expected value:" + id2 ).isTrue();
}

@Entity(name = "TestEntity")
public static class TestEntity {

@Id
private Long id;

private String name;

public TestEntity() {
}

public TestEntity(Long id, String name) {
this.id = id;
this.name = name;
}
}

@Entity(name = "AnotherTestEntity")
public static class AnotherTestEntity {

@Id
private Long id;

private String name;

public AnotherTestEntity() {
}

public AnotherTestEntity(Long id, String name) {
this.id = id;
this.name = name;
}
}
}
Loading