I have found an issue when trying to query an embedded identifier property from an association that is mapped as an identifier of an entity that uses IdClass.
Given the following domain:
{code:java} @Entity(name = "BasicEntity") public static class BasicEntity { @Id Long key1; }
@Entity(name = "IdClassEntity") @IdClass( IdClassEntity.IdClassEntityId.class ) public static class IdClassEntity { @Id @ManyToOne BasicEntity basicEntity; @Id Long key2;
public static class IdClassEntityId implements Serializable { Long basicEntity; Long key2; } }
@Entity(name = "NestedIdClassEntity") @IdClass( NestedIdClassEntity.NestedIdClassEntityId.class ) public static class NestedIdClassEntity { @Id @ManyToOne IdClassEntity idClassEntity; @Id Long key3;
public static class NestedIdClassEntityId implements Serializable { IdClassEntity.IdClassEntityId idClassEntity; Long key3; } } {code}
The test:
{code:java} s.createQuery( "SELECT a.idClassEntity.basicEntity.key1 FROM NestedIdClassEntity a " ).getResultList();
{code}
will fail with:
{code:java} ava.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: key1 of: org.hibernate.test.idclass.NestedIdClassQueryTest$NestedIdClassEntity [SELECT a.idClassEntity.basicEntity.key1 FROM org.hibernate.test.idclass.NestedIdClassQueryTest$NestedIdClassEntity a ]
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.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) at org.hibernate.test.idclass.NestedIdClassQueryTest.lambda$test$0(NestedIdClassQueryTest.java:53) at org.hibernate.testing.transaction.TransactionUtil.doInHibernate(TransactionUtil.java:318) at org.hibernate.test.idclass.NestedIdClassQueryTest.test(NestedIdClassQueryTest.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 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.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:748) Caused by: org.hibernate.QueryException: could not resolve property: key1 of: org.hibernate.test.idclass.NestedIdClassQueryTest$NestedIdClassEntity [SELECT a.idClassEntity.basicEntity.key1 FROM org.hibernate.test.idclass.NestedIdClassQueryTest$NestedIdClassEntity a ] 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) ... 19 more Caused by: org.hibernate.QueryException: could not resolve property: key1 of: org.hibernate.test.idclass.NestedIdClassQueryTest$NestedIdClassEntity at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:73) at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:67) at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1982) at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:393) at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:509) at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:686) at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:264) at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:204) at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114) at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109) at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104) at org.hibernate.hql.internal.ast.tree.DotNode.resolveSelectExpression(DotNode.java:748) at org.hibernate.hql.internal.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:1057) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2295) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2232) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1503) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:585) 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) ... 25 more {code}
Because {{ idClassEntity.basicEntity.key1 }} is not added as property path in the {{ BasicEntityPropertyMapping }} . This is because {{ idClassEntity.basicEntity }} is treated as a basic type , rather than an association type. This is most likely caused by the handling of attributes that are derived from {{@ IdClass }} attributes in the metamodel, as reported in HHH- 12842 12969 , although it might be possible to work around the issue without solving HHH- 12842 12969 (which would probably require some bigger rework of the IdClass related code). |
|