Where clause with equality on entities not on ids / Hibernate 6 vs 5


Considering that I have a many-to-one relationship between Post and Category objects, and we want to find all Posts related to a given Category.

The following query works: “from Post p where p.category=:category”
No need to explain that it will be done through ids.

Considering now that we have @MappedSuperclass SuperPost and SuperCategory and that we use generic, the query “from SpecificPost p where p.category=:category” doesn’t work with Hibernate 6, but it does with Hibernate 5.

For this to work, we have to specify ids like that: “from SpecificPost p where p.category.id=:categoryId”

See the unit test in the next comment.

Is it a bug (regression) or an improvement to fit the JPA specification?



There is an open discussion on whether we still want to support this behavior: HHH-15802 ubQuery with "in" results in java.lang.ClassCastException: class org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl cannot be cast to class org.hibernate.metamodel.mapping.EntityValuedModelPart by dreab8 · Pull Request #5800 · hibernate/hibernate-orm · GitHub

Comparing an entity valued path against some basic value is kinda strange. You can on the other hand keep your original query and use entityManager.getReference( Category.class, categoryId ) to fix this

Thank you Beikov.

After further research, I modify the topic because it appears when we query two entities that inherit of MappedSuperclass entities, with Generics and with a many-to-one relationship between them.

The code below is a real unit test that proves that there might be a bug in Hibernate 6.
It works with Hibernate 5.

The model may seem complicated but much of my code is based on it.

public class SuperCategory {

    private Long id;

    public SuperCategory() {}

    public SuperCategory(Long id) {this.id = id;}

public class SpecificCategory extends SuperCategory {

    public SpecificCategory() {}

    public SpecificCategory(Long id) {super(id);}

public class SuperPost<T extends SuperCategory> {

    private Long id;
    private T category;
    public SuperPost() {}

    public SuperPost(Long id) {this.id = id;}

    public T getCategory() {return category;}

public class SpecificPost extends SuperPost<SpecificCategory> {

    public SpecificPost() {}

    public SpecificPost(Long id) {super(id);}

public void with_id_OK() {
            "select count(p) from SpecificPost p where p.category.id=:categoryId")
            .setParameter("categoryId", 1L)

    // select count(s1_0.id) from SpecificPost s1_0 where s1_0.category_id=?

public void with_object_FAILS() {
    SpecificCategory category = new SpecificCategory(1L);

    em.getEntityManager().persist(new SpecificPost(1L));

    em.getEntityManager().createQuery("select count(p) from SpecificPost p where p.category=:category")
            .setParameter("category", category)

    // Error interpreting query [select count(p) from SpecificPost p where p.category=:category];
    // this may indicate a semantic (user query) problem or a bug in the parser
    //      [select count(p) from SpecificPost p where p.category=:category]

That could be a bug. Please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.

Hi beikov,

I implemented the unit test as requested and was surprised that it passed.

In fact, the bug is fixed in the 6.1.7 version. The test does fail with version 6.1.6.

My project is based on Spring Boot 3.0.2 which depends on hibernate version 6.1.6.
I didn’t think to upgrade to 6.1.7 before posting this topic.

Thank you very much for your help.


1 Like