mzr
mzr

Reputation: 69

Spring Data JPA: Hibernate Query Cache is invalidated even if unrelated records are added, modified or deleted

Hibernate Query Cache is getting invalidated even if unrelated record is added, deleted or modified.

Entity

@Entity
@Cache(region = "book", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  public String category;
}

Repository

@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
  @QueryHints({@QueryHint(name = org.hibernate.jpa.QueryHints.HINT_CACHEABLE, value = "true")})
  Iterable<Book> findAllByCategory(String category);
}

Testing

If I fetch book by one category (Novel), data is returned from cache from second call. But when I add/modify/delete any record for other category, previous query cache is invalidated.

Expectation

I am expecting that a query cache should be invalidated only if record matching bind parameter are added/deleted/modified.

Update 1

On further testing, I observed that cache is invalidated if there change in any table (Even the tables for which Query Cache is not enabled)

Upvotes: 1

Views: 74

Answers (0)

Related Questions