Entity A has two unidirectional & lazy one-to-one relationships to B (i.e. there’s no inverse relationship).
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "first_b_id")
private B firstB;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "second_b_id")
private B secondB;
Lazy-loading works fine. BUT if I invoke
myA.getSecondB()
the SQL log shows that Hibernate issues something like
select * from B where id in (value1, value2)
Hence, the relationships are treated lazily but they’re all loaded at once.
What are the explanations and work-arounds for this?
Oh, why is that so relevant you may ask… Well, the B entities are LOBs. One is the 5+MB original, the other is a 200KB “preview”. Of course I only want to load the original if absolutely necessary.
The explanation is probably that you enabled batch fetching for the class B (or for every entity using the
hibernate.default_batch_fetch_sizeconfiguration property).