Constellation / Problem I have the following entities/objects
- MasterEntity
- ChildEntity
- RelatedEntity
- ChildPojo (used for the query)
See https://postimg.cc/JH8h5t4h (unfortunately I cannot upload any pictures to this Jira board) The MasterEntity has the interhitance strategy JOINED.
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class MasterEntity {
}
The ChildEntity extends the MasterEntity and has a (bi-directional) one-to-many relatio to RelatedEntity.
@Entity
public class ChildEntity extends MasterEntity {
@OneToMany(mappedBy = "childEntity")
public Set<RelatedEntity> relatedEntitySet;
}
Additionally there is a ChildPojo, which is used in a criteria query. When executing now a query with a join to RelatedEntity, then I get an exception, as it looses the join to the MasterEntity. Criteria Query
@Transactional
List<ChildPojo> getByQueryWithAdditionalJoin() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<ChildPojo> query = cb.createQuery(ChildPojo.class);
Root<ChildEntity> root = query.from(ChildEntity.class);
Join<ChildEntity, RelatedEntity> relatedEntityJoin = root.join("relatedEntitySet");
query.multiselect(
root.get("masterField"),
root.get("childField"),
relatedEntityJoin.get("relatedField")
);
TypedQuery<ChildPojo> result = em.createQuery(query);
List<ChildPojo> resultList = result.getResultList();
return resultList;
}
Generated Query
Here the join to the MasterEntity is missing. Exception (tries to get a field for a not known alias)
Without the (manual) join, the query looks like expected (but of course without the needed join to the RelatedEntity):
In Hibernate 5.6.15.Final the same constellation (with the additional join) works fine:
I created two reproducers for this - one with Hibernate 5 and one with Hibernate 6. Both uses Quarkus, but the error can also be reproduced with “plain” Hibernate. Simply execute a mvn clean install and you will get the error for the Hibernate 6 project. The Hibernate 5 project works as expected.
Hint: Possibly this is related to https://hibernate.atlassian.net/browse/HHH-16967 |