# Unexpected behaviour when using Hibernate Filter and Batching

**URL:** https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923
**Category:** Hibernate ORM
**Created:** [July 5, 2023, 11:34am UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923 "2023-07-05T11:34:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![akshitagarwal](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/akshitagarwal/32/2266_2.png) [@akshitagarwal](https://discourse.hibernate.org/u/akshitagarwal)
#### Post date: [July 5, 2023, 11:34am UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/1 "2023-07-05T11:34:48Z")

</div>

We have 2 entities: Entity A and Entity B and we have a Hibernate Filter on Entity A.  
EntityA.xml

```auto
<set name="EntityBSet" inverse="true" access="property" batch-size="100">
        	<key column="A_FK" />
        	<one-to-many class="B" />
        	<filter name="TEST_FILTER" condition=" A_COUNTRY=:country "/>
</set>

```

Now if we try to load the EntityBSet when Filter is applied, because of Hibernate Batching it add the condition A\_COUNTRY in where clause, because of which all EntityB are not loaded. This is leading to data mismatch issues.

Example:  
Suppose we fetch 2 instances of Entity A, a1 and a2. And then we enable Hibernate Filter and then we do a1.getEntityBSet, this fires a query:

```auto
/* load one-to-many EntityBSet */ select * from EntityB where A_COUNTRY=country1 and A_FK in (?, ?);

```

But if a1 and a2 are of different country, it won’t load EntityBSet for a2 and instead will say that it has 0 EntityB.

And even later if I change my Hibernate Filter to country2, it will still give 0 results.

---

<div class="post-metadata">

### Author: ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)
#### Post date: [July 5, 2023, 1:03pm UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/2 "2023-07-05T13:03:17Z")

</div>

You are setting the filter parameter `:country` to a specific value. When the associations are retrieved, the parameter value currently set is the one that’s used in the query.

If you don’t want your filter applied when retrieving associations, but only when querying `EntityA` instances, then you should remove it from the `EntityBSet` mapping.

---

<div class="post-metadata">

### Author: ![akshitagarwal](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/akshitagarwal/32/2266_2.png) [@akshitagarwal](https://discourse.hibernate.org/u/akshitagarwal)
#### Post date: [July 5, 2023, 1:26pm UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/3 "2023-07-05T13:26:52Z")

</div>

I want to use it while retrieving associations also. But it should initialise the EntityB objects properly.  
For example:

Suppose we have 2 Entity A and Entity B records:  
Entity A:  
A1  
A2

Entity B:  
B1, A1, Country1  
B2, A2, Country2

1. Get all entity A  
This will return both A1, A2
2. Set Country Filter to Country1
3. Load EntityBSet for A1  
Here this query is fired

```auto
/* load one-to-many EntityBSet */ select * from EntityB where A_COUNTRY=country1 and A_FK in (A1, A2);

```

1. Set Country Filter to Country2
2. Load EntityBSet for A2  
Here, no query is fired and it says A2.EntityBSet is empty even though it has 1 record.

---

<div class="post-metadata">

### Author: ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)
#### Post date: [July 6, 2023, 9:29am UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/4 "2023-07-06T09:29:13Z")

</div>

Looks like you have batch fetching enabled. As you can see, Hibernate lods the `EntityB` for both the `EntityA` that were previously fetched. If you want to execute a single query for each association, each time respecting the filter parameter you’ve set, you should disable batch fetching for that association.

---

<div class="post-metadata">

### Author: ![akshitagarwal](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/akshitagarwal/32/2266_2.png) [@akshitagarwal](https://discourse.hibernate.org/u/akshitagarwal)
#### Post date: [August 29, 2023, 1:42pm UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/5 "2023-08-29T13:42:58Z")

</div>

@mbladel Is it possible to disable batching at a session level?

---

<div class="post-metadata">

### Author: ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)
#### Post date: [August 29, 2023, 1:59pm UTC](https://discourse.hibernate.org/t/unexpected-behaviour-when-using-hibernate-filter-and-batching/7923/6 "2023-08-29T13:59:41Z")

</div>

In Hibernate `6.3.0` we have introduced a `Session#setFetchBatchSize` that, when passed `0` as batch size, will effectively disable batching for the specific session.

I would still suggest always disabling batch-fetching for you association though, and not only in a specific session, as long as you’re using a parameter to filter its values.
