I have an issue with a criteria query in Spring Boot 3.1.2 (Hibernate 6.2.6).
Description of Mapping
Assuming we have two entities EntityA
and EntityB
. EntityA
has a simple integer ID as primary key. EntityB
has a composite key, consisting of one related EntityA
and a string. Given by the mapping, there can be multiple entities B
for one entity A
(n:1).
The following diagram shows the entities, their attributes, and the relationship between them:
Criteria Query
We want to build a criteria query to select all Entities A
, which have a relationship with an EntityB
that have a specific value in the string attribute of their composite key.
For that, we use the following (exemplary) query:
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<EntityA> cq = cb.createQuery(EntityA.class);
final Root<EntityA> entityARoot = cq.from(EntityA.class);
final ListJoin<EntityA, EntityB> entityBJoin = entityARoot.join(EntityA_.entitiesB);
cq.where(cb.equal(
entityBJoin.get(EntityB_.key).get(EntityBKey_.id),
"foo"
));
cq.select(entityARoot);
em.createQuery(cq).getResultList();
Problem
When executing the query, I get the following exception:
org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup - com.example.demo.EntityA(10532809360900).entitiesB(10532811315300).{element}.key
at org.hibernate.sql.ast.spi.FromClauseAccess.getTableGroup(FromClauseAccess.java:52)
at org.hibernate.query.sqm.sql.internal.BasicValuedPathInterpretation.from(BasicValuedPathInterpretation.java:54)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.lambda$visitBasicValuedPath$49(BaseSqmToSqlAstConverter.java:4204)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.prepareReusablePath(BaseSqmToSqlAstConverter.java:3606)
Does anybody know if there is something wrong with our mapping or query? Or is this a Hibernate issue?
It is worth mentioning that working with the entities (e.g., retrieved by the repository classes) works well and as expected.