As a workaround, In the previous project, I decided to map multiple entities to a single table. In this way, I can choose what attributes are eagerly loaded. For listing purposes, I can use OrderSummary`. Because the list usually doesn’t require much. But on the detailed page, I can use OrderDetailed` It will load attributes via additional select.
@Entity
@Table(name = "order")
@Immutable
class OrderSummary {
@Id
private Long id;
private BigDecimal total;
}
@Entity
@Table(name = "order")
class OrderDetailed {
@Id
private Long id;
private BigDecimal total;
@Fetch(FetchMode.SUBSELECT)
@OneToMany(fetch = FetchType.EAGER)
private List<OrderItem> items;
@OrderBy("createdAt DESC")
@Fetch(FetchMode.SUBSELECT)
@OneToMany(fetch = FetchType.EAGER)
private List<OrderHistoryRecord> history;
}
I know I could load attributes via Hibernate.initialize(). But initialization is better to happen at the persistence layer. I don’t understand why no one is interested that configuration controlled sub select fetching is can be useful. |