Hibernate version 6.2.6
I have a hibernate entity called Product, This entity has an ElementCollection to link interested buyers. The INTERESTED_BUYERS is a table containing an id of the interested buyer(interested_buyer_id) and a column used as foreign key reference to Product table (item_id).
The relationship is defined as follows:
@ElementCollection(targetClass = String::class, fetch = FetchType.EAGER)
@CollectionTable(name = "INTERESTED_BUYERS", joinColumns = [JoinColumn(name = "item_id")])
@Column(name = "interested_buyer_id", nullable = false)
@Fetch(FetchMode.SELECT)
var interestedBuyerIds: MutableSet<String>,
It’s completely acceptable situation that there exists a Product that has no interested buyers linked to it.
Now the problem I’m experiencing is that when I’m fetching Product entities from the database as I’ve inspected Hibernate logs I can see that hibernate does the additional query to find the interested buyers, as expected like this:
select i1_0.item_id,i1_0.interested_buyer_id from INTERESTED_BUYERS i1_0 where i1_0.case_id = any (?)
But when looking at the parameters for this query there is
TRACE: binding parameter [1] as [ARRAY] [[0cf4abb9-2e93-4b5b-8b82-8ce73bfb10e8, 2b543a8e-c024-4f96-8d87-a698675ac749, 4de28f35-7d7c-4961-8a3a-47584bedef80, c9cd470b-a7c0-4e3b-8806-cc3c1ac566a7, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
I then realized that we had a certain default_batch_fetch_size set and that was the culprit of this. Meaning the fetch fetched the elements as it should but since there were less elements to fetch than the default batch size was hibernate populated the rest of the fetch query with nulls.
I now realize that my default_batch_fetch_size configuration was quite stupid as I lacked the understanding on the repercussions. I have since lowered it to match what actually is being fetched, But Is this really the expected behaviour with default_batch_fetch_size? Or is there some other misconfiguration? Should I ditch the property altogether and use something else?
Lowering the default_batch_fetch_size got rid of the filler nulls. But I’m still quite confused about this behaviour. Expectation was that setting the default_batch_fetch_size property wouldn’t make the sub fetches working in this illogical fashion