I am really interested in how Hibernate invalidates this query cache, both in the current version 6.2 and in the future version 6.5.
Hibernate ORM knows all the “query spaces” i.e. tables that are touched for a query. Whenever one of the tables that are involved in a query is mutated, Hibernate ORM “invalidates” all cache entries. The invalidation is very coarse, so you shouldn’t use caching if you expect a lot of changes to the underlying tables. Also see Hibernate ORM User Guide for more information.
- Does Hibernate take the
EntityGraph
into account to track related entities for cache invalidation?
Yes
- Does Hibernate invalidate the query cache every time an existing
Comment
gets updated or deleted?
Yes
- Does Hibernate depend somehow on a specific way
Comment
gets updated, e.g. viadirty check
, or is there no concern at all?
No
- Does Hibernate take care of all possibilities and can I rely on Hibernate, so that
Comment
s in the related collection of thePost
entity are always up to date when I use the query cache, even when the collection is markedFetchType.LAZY
?
Yes
- Am I right that starting with Hibernate v6.5 all these concerns will disappear, since from this point on Hibernate will only store the related entity-id in the query cache and will then try to rehydrate the entity directly from its dedicated entity cache?
Not sure which specific concerns you have. Can you please restate them?
- Will I have to annotate the collection in the Post entity for proper collection caching, even when I only use query cache?
Yes, you should do that already if you want the collection entries to be cached. Also see Hibernate ORM User Guide