How does Hibernate invalidate Query-Cache?

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.

  1. Does Hibernate take the EntityGraph into account to track related entities for cache invalidation?

Yes

  1. Does Hibernate invalidate the query cache every time an existing Comment gets updated or deleted?

Yes

  1. Does Hibernate depend somehow on a specific way Comment gets updated, e.g. via dirty check, or is there no concern at all?

No

  1. Does Hibernate take care of all possibilities and can I rely on Hibernate, so that Comments in the related collection of the Post entity are always up to date when I use the query cache, even when the collection is marked FetchType.LAZY?

Yes

  1. 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?

  1. 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

1 Like