I've noticed a strange behavior while using BatchSize/Prefetching on the inverse side (mappedBy) of a OneToMany relation.
Entity A (1) <---> (0..*) Entity B
Code:
class A {
...
@OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
@BatchSize(size=50)
public Set<B> bs;
}
class B {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_FK", nullable = false, unique = false)
public A a;
}
While iterating over a number of A entities and accessing their "bs", Hibernate (pre-)fetches the "bs" collections lazily.
Case 1: OKIf the number of uninitialized collection proxies equals the defined BatchSize of 50, Hibernate uses only a single query to initialize all 50 proxies as expected.
> select ... where ... in (?, x50)Case 2: ?? But when the number of uninitialized collection proxies is lower than the defined BatchSize (e.g. 45 proxies), Hibernate suddenly needs 3 queries to initialize all proxies.
> select ... where ... in (?, x23)
> select ... where ... in (?, x11)
> select ... where ... in (?, x11)My expectation would have been, that Hibernate uses a single query with an IN set of 45 elements instead.
What's the idea of this behavior?
From reading
http://docs.jboss.org/hibernate/core/3. ... mance.html "19.1.5. Using batch fetching" I would have expected that the size of the IN sets should be
min(#uninitializedProxies, BatchSize).