Disabling filter's defaultCondition using boolean parameter doesn't work

My project used to be in Springboot 3.0.2 which had Hibernate 6.1, when upgrading to the most recent version, Hibernate got updated to 6.6, and my @Where clauses which in previous versions wouldn’t work with LEFT JOIN FETCH, now do work ( which broke some tests), and I needed to implement a way where I could disable the clause dinamically (to avoid refactoring all of my project’s queries), so I tried using filters:

@FilterDef(
name = “softDelete”,
parameters = @ParamDef(name = “fetchDeleted”, type = Boolean.class, resolver = HibernateFilterSupplier.class),
defaultCondition = “(:fetchDeleted = true OR deleteTimeStamp IS NULL)”,
autoEnabled = true
)
@Filter(name = “softDelete”)

The idea would be: if fetchDeleted property is set to true, then the deleteTimeStamp IS NULL condition wouldn’t be used. Here is my supplier class:

public class HibernateFilterSupplier implements Supplier {

private static final ThreadLocal<Boolean> fetchDeleted = ThreadLocal.withInitial(() -> false);

public static void setFetchDeleted(boolean value) {
    fetchDeleted.set(value);
}

@Override
public Boolean get() {
    return fetchDeleted.get();
}

}

With this implementation, I would call: HibernateFilterSupplier.setFetchDeleted(true); when I wanted to fetch deleted rows. But this doesn’t work, I also tried scrapping this parameter idea to create a global filter ( implement Interceptor interface), and then later disable it for a specific query and then re-enable it, but that also didn’t work.

I think you are affected by HHH-19903 since the filter value is stored in the SQL AST.

Interesting, that might be the case, I’m running hibernate 6.6.33. Are there any workarounds at the moment for this problem?

The only thing that you can do is to avoid using parameters i.e. have two filters and enable one or the other. I think you could also keep on auto-enabled and disable the filter via Session#disableFilter