Imagine the following example
class PolymorphicStart {
@Id @GeneratedValue Integer id;
@ManyToOne(fetch = LAZY) PolymorphicIdRelation idRelation;
}
class PolymorphicIdRelation {
@EmbeddedId PolymorphicIdRelationId id;
}
class PolymorphicIdRelationId {
String name;
@ManyToOne(fetch = LAZY) PolymorphicBase base;
}
@Inheritance(strategy = SINGLE_TABLE)
abstract class PolymorphicBase {
@Id @GeneratedValue Long id;
String name;
}
class PolymorphicSub1 extends PolymorphicBase {
}
class PolymorphicSub2 extends PolymorphicBase {
}
with the following query
SELECT start
FROM PolymorphicStart start
LEFT JOIN FETCH start.id.idRelation r
LEFT JOIN FETCH r.base b
The attached example shows that the relation base in the embedded id PolymorphicIdRelationId is not properly initialized with the real instance, but instead a proxy although I am using a fetch join in the query. This is especially problematic when the type of the relation is just a base type in an inheritance hierarchy. Note that I also tested duplicating the base relation to the entity PolymorphicIdRelation, but that didn't help either. The duplicate was initialized with a proxy too, although I was join fetching that entity relation instead of the relation within the id!
The only thing that actually worked was converting the relation base in the embedded id into a basic attribute(Long baseId in my case) and map the relation with insertable and updatable set to false on the entity level.
|