SQLGrammarException / "Column ... not found" for superclass query when multiple subclasses contain a property by the same name in Hibernate
Issue
- In a class hierarchy with multiple inheritance strategies, there is a
ManyToOne
association 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) ...
Environment
- Red Hat JBoss Enterprise Application Platform (EAP) 7
- Hibernate 5
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.