| With entities
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "E1")
public class MyEntity1 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
}
extended by
@Entity
@DiscriminatorValue(value = "E2")
public class MyEntity2 extends MyEntity1 {
...
}
With data prepared so that there is a instance of MyEntity2 persisted and the following findOne works:
MyEntity2 theEntity = (MyEntity2) em.find(MyEntity1.class, id);
Will result in a ClassCastException with the findOne in the following:
MyEntity1 theRef = em.getReference(MyEntity1.class, id); MyEntity2 theEntity = (MyEntity2) em.find(MyEntity1.class, id);
This seems to be due to the reference first being stored in the session with only the superclass, causing the surprise in the call to find later. This makes it so that entities loaded with getReferences cannot be used where references loaded with find can be used, even when later loaded using find.
- hibernate-core:5.0.12.Final
- hibernate-jpa-2.1-api:1.0.0.Final
Suggested improvement: When getting a reference to a class with @Inheritance, make a database call to check the @Discriminator so that the correct type of proxy can be returned. (somewhat related to this, I'm seeing a change in behavior in how getReference works ... is used to be that a null would be returned when called with a non-existent ID, but now a reference is returned that causes an exception when properties are accessed ... this seems ok to me, but may be noteworthy) FYI, a related issue was created in spring-boot https://jira.spring.io/browse/DATAJPA-1206, but closed as "works as intended" ... |