Using an unmapped discriminator value in HQL

Hello,
I’m upgrading an application from Hibernate 5.6 to 6.2 and running into the case where a query fails with an error:

session
.createQuery("select s.code, type(s) from MyEntity s where s.class != 'XYZ'")
.list()

The "XYZ" value does not correspond to any mapped class, from what I understood Hibernate tries to find the java class for that discriminator value and that’s where the exception occurs:

java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.DiscriminatorValueDetails.getIndicatedEntity()" because "discriminatorDetails" is null
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitLiteral(BaseSqmToSqlAstConverter.java:5309) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitLiteral(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.tree.expression.SqmLiteral.accept(SqmLiteral.java:65) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:7316) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate.accept(SqmComparisonPredicate.java:104) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitWhereClause(BaseSqmToSqlAstConverter.java:2471) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:2048) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.tree.select.SqmQuerySpec.accept(SqmQuerySpec.java:125) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitQueryPart(BaseSemanticQueryWalker.java:221) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQueryPart(BaseSqmToSqlAstConverter.java:1902) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:1587) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.tree.select.SqmSelectStatement.accept(SqmSelectStatement.java:222) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.translate(BaseSqmToSqlAstConverter.java:770) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:345) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:268) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:244) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:518) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
	at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:367) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
...

Can you please advise if that use-case is no longer supported in 6.2 (or if it was never supposed to work)?

You can create JIRA ticket for this, but I seriously doubt that this was ever considered supported and just worked by chance. You should rather spell out the entity name for the thing that has the discriminator XYZ i.e. s.class != com.mycompany.MyEntityXyz.

1 Like

Thanks for the answer, I’ll change my code so it does not use something that wasn’t really supposed to work