Hello @beikov ,
Thanks for reply, I was able to reproduce using the test template. However, I’m not able to create an issue on the issue tracker. Whenever I click the “Create” button I get an error. Maybe something is blocked on my network. Can you please assist me in creating the ticket.
This is my reproducer hibernate-test-case-templates/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java at main · khaledbaghdadii/hibernate-test-case-templates
I’ll paste below the ticket title and description.
Title:
NPE in BaseSqmToSqlAstConverter.withTreatRestriction when using treat() with @ElementCollection join on SINGLE_TABLE inheritance
Description:
When using CriteriaBuilder.treat() to downcast a Root to a subclass entity, and then joining
an @ElementCollection field on that treated root, Hibernate throws a NullPointerException
because “elementTableGroup” is null.
Environment:
- Hibernate ORM 7.x
- H2 / PostgreSQL
Entity Setup:
- Parent entity with
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
- Child entity with
@ElementCollection (Set)
Code that fails:
CriteriaBuilder cb = em.getCriteriaBuilder();CriteriaQuery query = cb.createQuery(ParentEntity.class);Root root = query.from(ParentEntity.class);
// This line causes NPEPath path = cb.treat(root, ChildEntity.class).join(“itemIds”, JoinType.LEFT);
query.select(root).distinct(true).where(path.in(“item-A”));em.createQuery(query).getResultList(); // NPE here
Stack trace:
2026-02-04 15:29:41 INFO jpa:102 - HHH008540: Processing PersistenceUnitInfo [name: templatePU]
2026-02-04 15:29:41 INFO core:41 - HHH000001: Hibernate ORM core version 7.2.3.Final
2026-02-04 15:29:41 INFO core:143 - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.format_sql=true, jakarta.persistence.validation.mode=NONE, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1, hibernate.max_fetch_depth=5, hibernate.show_sql=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.driver_class=org.h2.Driver, hibernate.connection.password=****, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.pool_size=5, hibernate.cache.region.factory_class=org.hibernate.testing.cache.CachingRegionFactory, hibernate.service.allow_crawling=false, hibernate.session.events.log=true}
2026-02-04 15:29:41 WARN CachingRegionFactory:46 - org.hibernate.testing.cache.CachingRegionFactory should be only used for testing.
2026-02-04 15:29:41 INFO cache:49 - HHH90001028: Second-level cache region factory [org.hibernate.testing.cache.CachingRegionFactory]
2026-02-04 15:29:41 WARN pooling:85 - HHH10001002: Using built-in connection pool (not intended for production use)
2026-02-04 15:29:42 WARN deprecation:152 - HHH90000025: H2Dialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
2026-02-04 15:29:42 INFO pooling:179 - HHH10001005: Database info:
Database JDBC URL [jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1]
Database driver: H2 JDBC Driver
Database dialect: H2Dialect
Database version: 2.4.240
Default catalog/schema: DB1/PUBLIC
Autocommit mode: false
Isolation level: READ_COMMITTED
JDBC fetch size: 100
Pool: DriverManagerConnectionProvider
Minimum pool size: 1
Maximum pool size: 5
2026-02-04 15:29:43 INFO core:56 - HHH000490: Using JTA platform [org.hibernate.engine.transaction.jta.platform.internal.WildFlyStandAloneJtaPlatform]
Hibernate:
drop table if exists child_item_ids cascade
Hibernate:
drop table if exists parent_entity cascade
Hibernate:
create table child_item_ids (
child_id varchar(255) not null,
item_id varchar(255)
)
Hibernate:
create table parent_entity (
entity_type varchar(31) not null check ((entity_type in ('JPAUnitTestCase$ParentEntity','CHILD'))),
id varchar(255) not null,
name varchar(255),
primary key (id)
)
Hibernate:
alter table if exists child_item_ids
add constraint FKkpxl9trkcrwwqs8xe53w9qpkm
foreign key (child_id)
references parent_entity
Hibernate:
insert
into
parent_entity
(name, entity_type, id)
values
(?, 'CHILD', ?)
Hibernate:
insert
into
parent_entity
(name, entity_type, id)
values
(?, 'CHILD', ?)
Hibernate:
insert
into
child_item_ids
(child_id, item_id)
values
(?, ?)
Hibernate:
insert
into
child_item_ids
(child_id, item_id)
values
(?, ?)
Hibernate:
insert
into
child_item_ids
(child_id, item_id)
values
(?, ?)
Hibernate:
insert
into
child_item_ids
(child_id, item_id)
values
(?, ?)
Hibernate:
drop table if exists child_item_ids cascade
Hibernate:
drop table if exists parent_entity cascade
2026-02-04 15:29:43 ERROR pooling:148 - Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1
2026-02-04 15:29:43 ERROR pooling:341 - HHH10001023: Connection leak detected: there are 1 unclosed connections
java.lang.NullPointerException: Cannot invoke "org.hibernate.sql.ast.tree.from.TableGroup.getModelPart()" because "elementTableGroup" is null
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.withTreatRestriction(BaseSqmToSqlAstConverter.java:5336)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.createExpression(BaseSqmToSqlAstConverter.java:4461)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitTableGroup(BaseSqmToSqlAstConverter.java:4234)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQualifiedAttributeJoin(BaseSqmToSqlAstConverter.java:4149)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQualifiedAttributeJoin(BaseSqmToSqlAstConverter.java:466)
at org.hibernate.query.sqm.SemanticQueryWalker.visitSetJoin(SemanticQueryWalker.java:232)
at org.hibernate.query.sqm.tree.domain.SqmSetJoin.accept(SqmSetJoin.java:85)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitInListPredicate(BaseSqmToSqlAstConverter.java:8179)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitInListPredicate(BaseSqmToSqlAstConverter.java:466)
at org.hibernate.query.sqm.tree.predicate.SqmInListPredicate.accept(SqmInListPredicate.java:154)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitWhereClause(BaseSqmToSqlAstConverter.java:2550)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.querySpec(BaseSqmToSqlAstConverter.java:2083)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:2056)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:466)
at org.hibernate.query.sqm.tree.select.SqmQuerySpec.accept(SqmQuerySpec.java:127)
at org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitQueryPart(BaseSemanticQueryWalker.java:253)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQueryPart(BaseSqmToSqlAstConverter.java:1939)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:1638)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:466)
at org.hibernate.query.sqm.tree.select.SqmSelectStatement.accept(SqmSelectStatement.java:251)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.translate(BaseSqmToSqlAstConverter.java:808)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildInterpretation(ConcreteSqmSelectQueryPlan.java:469)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:384)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:355)
at org.hibernate.query.sqm.internal.SqmQueryImpl.doList(SqmQueryImpl.java:374)
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:153)
at org.hibernate.query.Query.getResultList(Query.java:121)
at org.hibernate.bugs.JPAUnitTestCase.testTreatWithElementCollectionJoin(JPAUnitTestCase.java:79)
Expected behavior:
The treat() + join() on @ElementCollection should work and produce a valid SQL query
with proper joins to the collection table.
Actual behavior:
NullPointerException during SQM to SQL AST conversion.
Reproducer attached: hibernate-test-case-templates/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java at main · khaledbaghdadii/hibernate-test-case-templates