Consider the following abridged model (please note the reused food_id column and the self-referencing @ManyToOne in Fruit and Cheese):
class Container {
}
class FruitContainer extends Container {
@ManyToOne
@JoinColumn(name = "food_id")
@Fetch(FetchMode.SELECT)
private Fruit fruit;
}
class CheeseContainer extends Container {
@ManyToOne
@JoinColumn(name = "food_id")
@Fetch(FetchMode.SELECT)
private Cheese cheese;
}
class Food {
}
class Fruit extends Food {
@ManyToOne
@Fetch(FetchMode.SELECT)
private Fruit bestPairedWith;
}
class Cheese extends Food {
@ManyToOne
@Fetch(FetchMode.SELECT)
private Cheese bestPairedWith;
}
When batch-loading several Container objects, Hibernate seems to create the wrong class (a Cheese instead of a Fruit) I’m not sure if reusing a column in different branches of an inheritance tree is supported but this seemed to work in Hibernate 5. In Hibernate 6 an initializer seems to be associated with the column during the batch load but it only works for one of the types. |