SQLGrammarException / "Column ... not found" for superclass query when multiple subclasses contain a property by the same name in Hibernate
Environment
- Red Hat JBoss Enterprise Application Platform (EAP) 7
- Hibernate 5
Issue
-
In a class hierarchy with multiple inheritance strategies, there is a
ManyToOneassociation in a join table entity within the hierarchy. -
A criteria query is used to load a subset of the entities within the hierarchy based on a property value of the top level entity type:
Criteria criteria = session.createCriteria(TheTopLevelEntityClass.class, "r"); criteria.add(Restrictions.eq("propertyOne", "some_value")); List list = criteria.list(); -
When the query is executed, there are two selects. The second select fails to include the foreign key column in the subclass table
-
Hibernate attempts to find the column value in a row and fails with:
org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) at org.hibernate.loader.Loader.doList(Loader.java:2620) at org.hibernate.loader.Loader.doList(Loader.java:2600) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429) at org.hibernate.loader.Loader.list(Loader.java:2424) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1774) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363) ... Caused by: org.h2.jdbc.JdbcSQLException: Column "TASK2_111_0_" not found ... ... at org.h2.jdbc.JdbcResultSet.getLong(JdbcResultSet.java:655) at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:169) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2738) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655) at org.hibernate.loader.Loader.getRow(Loader.java:1544) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727) at org.hibernate.loader.Loader.processResultSet(Loader.java:972) at org.hibernate.loader.Loader.doQuery(Loader.java:930) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) at org.hibernate.loader.Loader.doList(Loader.java:2617) ...
Resolution
This is a known defect (Content from issues.jboss.org is not included.JBEAP-7953) which is scheduled to be resolved in EAP 7.0 cumulative patch (CP) 05.
Two workarounds have been found to address some tested cases:
- Rename the property which appears in multiple subclasses
- The
fetchtype for the association can be specified asjoin
Root Cause
The failure is due to incorrect handling when two subclasses have the same property name, with one subclass using a secondary table for that property.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.