I create union query with two tables. This two tables have set of identical columns and set of not identical columns. I want that null values is selected for not identical columns.
I create query as follow (simplified, other columns were ommited):
List<UnitedAlarmDto> resultList = entityManager.createQuery("""
SELECT
ja.jobId as jobId
from JobAlarm ja
union all
SELECT
null as jobId
from SystemAlarm sa
""", Tuple.class)
.getResultList()
.stream()
.map(AlarmService::fromTuple).toList();
This query throws exception:
Caused by: org.hibernate.query.SemanticException: Select items of the same index must have the same java type across all query parts
at org.hibernate.query.sqm.tree.select.SqmQueryGroup.validateQueryGroupFetchStructure(SqmQueryGroup.java:174) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.sqm.tree.select.SqmQueryGroup.validateQueryStructureAndFetchOwners(SqmQueryGroup.java:154) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:403) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:311) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:71) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:165) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:147) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:790) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:840) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
... 251 common frames omitted
What is right way to create such query? Is it possible?
Tested on hibernate version: 6.4.4