Hello I am having difficult time resoning about filters and their usage in Hibernate. I am at the moment testing / playing around with filters and from my understanding we generally use two different types of filters. For this example assume we have a 1:1 relation between opening a session and a new explicit (require_new) transaction.
- The first one, the one we usually put on top of our entities, which we usually use to filter entire entities based upon some sort of condition. For example, the most basic one, the deleted flag use case
- The second one, the one we put inside or on a property of an entity, for example a collection, which is actually not filtering the root entity (that contains this property) but rather filtering the “contents” of that root entity (producing a different state of the same entity). For example a @OneToMany list where we filter out it’s contents by some property.
Now i am having a very serious issue with the way the 2nd types of filters work (the ones we put on properties). The problem here is that in a given session if we fetch an entity with the filter turned on, we would get that particular entity and it’s state/contents filtered (that is alright). But now the root entity is inside the persistence context, but it’s state is the filtered one (with the filter applied on one of it’s properties). Now in the same session i would like to get the same entity (retaining it’s non filterable properties the same) but with different filter turned on or the same filter as before but with different parameters (or any other permutation of the existing filters that can be applied on the CONTENTS of the entity, not filtering the entire entity itself). That wont work, because the persistence context already holds that root entity and will directly get it from there.
Now that means, that the persistence context holds the root entity in some sort of state (defined by one of it’s properties being filtered the first time when we extracted it). Say we have an entity with dozen simple/primitive properties (unfiltered, always the same, in a sence unmutable from the Filter’s perspective in that context) and just one @OneToMany which has the filter on it. In order for the user to extract the same entity within the same session with different filter on, or same filter with different parameters, he would have to evict the entity from the cache - therefore, we need to evict the entire entity from the cache with it’s dozen of properties already fetched which are never chaning or being filtered just to re-filter that single @OneToMany property again and produce a different state of the same root entity
I am probably missing something, or it’s just the way it works i am not sure. I vaguely remember somewhere in the docs where it was mentioned that the result of the filtered collection is not cached. Given how advanced hibernate is i woud’ve expected for it to be able to fetch the same entity with different filters turned on with minimal overhead. Given how much of the entity state would always remain the same, and a smaller part of it would be changed due to filtering, it has to have a better approach. I am sure @vlad can bring some much needed wisdom her
As of now this is not possible within the scope of a single session/transaction to load the same entity with different filters or same filter with different parameters turned on, unless you evict the first entity fetched first in order to change the filters which i don’t like, or the other way is to open a new session/transaction where you load the entity with the changed filters - but either way it would require us to not have any lazy loading on the filtered property or any other property for that matter in order to have both states of the same entity completely at hand/available after we finish loading them using the filters, otherwise once we evict from the cache in - the first approach, or the inner session closes - the second approach, and we go back to the parent one, we won’t be able to fetch the lazy properties from the entity since it will be detached (if we evict it to load the second state with different filters - first approach, or detached when session closes - the inner session approach)