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

**URL:** <https://discourse.hibernate.org/t/classcastexception-in-hibernate-6-when-join-fetch-is-used-in-a-query-with-entity-inheritance/7815>\
**Category:** Hibernate ORM\
**Created:** [June 14, 2023, 3:22pm UTC](https://discourse.hibernate.org/t/classcastexception-in-hibernate-6-when-join-fetch-is-used-in-a-query-with-entity-inheritance/7815 "2023-06-14T15:22:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@Victor](https://discourse.hibernate.org/u/Victor)\
**Post date:** [June 14, 2023, 3:22pm UTC](https://discourse.hibernate.org/t/classcastexception-in-hibernate-6-when-join-fetch-is-used-in-a-query-with-entity-inheritance/7815/1 "2023-06-14T15:22:38Z")

</div>

`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’)

```auto
@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;
}

```

```auto
@Entity
@Getter
@Setter
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class SuperClass implements Serializable {
    @Id
    public String id;

    @ManyToOne
    protected Entity1 entity1;
}

```

```auto
@Entity
@DiscriminatorValue("1")
public class SubClass1 extends SuperClass {
}

```

```auto
@Entity
@DiscriminatorValue("2")
public class SubClass2 extends SuperClass {
}

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 14, 2023, 3:34pm UTC](https://discourse.hibernate.org/t/classcastexception-in-hibernate-6-when-join-fetch-is-used-in-a-query-with-entity-inheritance/7815/2 "2023-06-14T15:34:26Z")

</div>

Try updating to 6.2.4.Final. If the problem persists, please create an issue in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)) with a test case([hibernate-test-case-templates/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java)) that reproduces the issue.
