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.