Selecting an @ElementCollection property via JPQL fails with an exception:
{code:sql}SELECT u.permissions FROM User u WHERE u.id = ?1{code}
throws: {{org.hibernate.QueryException: not an entity}}
Selecting a basic type or associated entity this way works as expected. Adding an explicit join fixes this:
{ noformat code:sql }SELECT p FROM User u JOIN u.permissions p WHERE u.id = ?1{ noformat code }
This *works*. The explicit JOIN is *not* needed for basic types and associated entities.
I've attached a test case using the Github hibernate/hibernate-test-case-templates, because I had some Gradle error when trying to build hibernate-orm directly. Which is a pitty, bc. I think this test could just be added to the existing [ElementCollection QueryTest|https://github.com/hibernate/hibernate-orm/blob/5.4.19/hibernate-core/src/test/java/org/hibernate/test/annotations/collectionelement/QueryTest.java] using the existing 'EntityWithAnElementCollection.class'.
Here is the complete Stack Trace for the test case:
{noformat}java.lang.IllegalArgumentException: org.hibernate.QueryException: not an entity [SELECT u.permissions FROM org.hibernate.bugs.SelectEntityCollectionTestCase$User u WHERE u.id = ?1]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103) at org.hibernate.bugs.SelectEntityCollectionTestCase.hhh14125Test(SelectEntityCollectionTestCase.java:93) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: org.hibernate.QueryException: not an entity [SELECT u.permissions FROM org.hibernate.bugs.SelectEntityCollectionTestCase$User u WHERE u.id = ?1] at org.hibernate.QueryException.generateQueryException(QueryException.java:120) at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:118) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:78) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:158) at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704) ... 18 more Caused by: org.hibernate.QueryException: not an entity at org.hibernate.hql.internal.ast.tree.FromElementType.renderIdentifierSelect(FromElementType.java:174) at org.hibernate.hql.internal.ast.tree.FromElement.renderIdentifierSelect(FromElement.java:208) at org.hibernate.hql.internal.ast.tree.SelectClause.renderNonScalarIdentifiers(SelectClause.java:464) at org.hibernate.hql.internal.ast.tree.SelectClause.renderNonScalarSelects(SelectClause.java:424) at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:247) at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1018) at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:786) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:677) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ... 24 more {noformat} |
|