Nested subtypes filtered out in bidirectional OneToMany association load

Description

Hello, I’m trying to migrate an application from Hibernate 5.5.8 to 6.4.0

Please consider the following abridged model:

class Refrigerator { @Id private Integer id; private String name; @OneToMany(targetEntity = Cheese.class, mappedBy = "refrigerator") private Set<Cheese> cheeses; } @Entity(name = "Food") @Table(name = "Food") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type") @DiscriminatorValue(value = "Food") class Food { @Id private Integer id; private String name; @ManyToOne(fetch = FetchType.LAZY) @Fetch(FetchMode.SELECT) @JoinColumn(name = "refrigerator_id") private Refrigerator refrigerator; } @Entity(name = "Cheese") @DiscriminatorValue("Cheese") class Cheese extends Food { } @Entity(name = "SmellyCheese") @DiscriminatorValue("SmellyCheese") class SmellyCheese extends Cheese { }

When trying to load the refrigerator.cheeses collection, Hibernate now generates the following SQL query:

select c1_0.refrigerator_id, c1_0.id, c1_0.type, c1_0.name from (select * from Food t where t.type='Cheese') c1_0 where c1_0.refrigerator_id=?

I think that Hibernate shouldn’t be filtering t.type='Cheese' but rather something like t.type in ('Cheese', 'SoftCheese')

In the end the collection only contains the entities of the actual type Cheese

I will submit a PR with a test case reproducing the problem

Activity

Show:

Guillaume ToisonNovember 29, 2023 at 1:58 PM

This might be a duplicate of HHH-17481

Guillaume ToisonNovember 29, 2023 at 12:59 PM

Pull request reproducing the problem:
https://github.com/hibernate/hibernate-orm/pull/7582

Fixed

Details

Assignee

Reporter

Worked in

Components

Sprint

Fix versions

Affects versions

Priority

Created November 29, 2023 at 11:44 AM
Updated January 25, 2024 at 12:51 PM
Resolved January 8, 2024 at 3:12 PM