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 {
}