ClassCastException in Hibernate 6 when "join fetch" is used in a query with entity inheritance

Description

em.createQuery("select e from " + Entity1.class.getSimpleName() + " e left join fetch e.subClass1 left join fetch e.subClass2 ", Entity1.class).getSingleResult()

This query used to work in Hibernate 5, but I'm getting the following error in Hibernate 6:

Exception in thread "main" java.lang.ClassCastException: class org.example.entity.SubClass1 cannot be cast to class org.example.entity.SubClass2 (org.example.entity.SubClass1 and org.example.entity.SubClass2 are in unnamed module of loader 'app')

@Entity @Getter @Setter public class Entity1 { @Id String id; @OneToOne(fetch = FetchType.LAZY, mappedBy = "entity1") protected SubClass1 subClass1; @OneToOne(fetch = FetchType.LAZY, mappedBy = "entity1") protected SubClass2 subClass2; }
@Entity @Getter @Setter @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public abstract class SuperClass implements Serializable { @Id public String id; @ManyToOne protected Entity1 entity1; }
@Entity @DiscriminatorValue("1") public class SubClass1 extends SuperClass { }
@Entity @DiscriminatorValue("2") public class SubClass2 extends SuperClass { }

Attachments

1
  • 14 Jun 2023, 04:02 PM

Activity

Show:

Seong Lee August 8, 2023 at 7:00 PM

Hello, i’m also in a similar situation and it is not working on 6.2.6.Final or 6.3.0.CR1.

Posted my case in a comment here: https://hibernate.atlassian.net/browse/HHH-16934?focusedCommentId=112562

Victor Mariano Lessa de Assis July 31, 2023 at 10:19 AM

Tested with version 6.3.0.CR1 and got the same error message as before.

Marco Belladelli July 31, 2023 at 7:29 AM

Hello , since https://hibernate.atlassian.net/browse/HHH-16494 was merged it would be nice if you could test if your issue persist with the latest version of Hibernate 6.3.0.CR1 that includes that fix.

Marco Belladelli June 20, 2023 at 2:59 PM

This might be related to https://hibernate.atlassian.net/browse/HHH-16494, as that issue is meant to address implicitly treated joins (i.e. that use subtypes without an explicit treat in the query) correctly.

Fixed

Details

Assignee

Reporter

Sprint

Fix versions

Affects versions

Priority

Created June 14, 2023 at 4:03 PM
Updated August 31, 2023 at 5:28 PM
Resolved August 11, 2023 at 1:13 PM