2nd level cache invalidation with native queries

前端 未结 1 1360
粉色の甜心
粉色の甜心 2021-01-24 13:20

I have an application that uses 2nd level cache on a JBoss AS 7 installation (Infinispan 2nd level cache provider).
We have some update JPQL Queries that invalidate the cach

相关标签:
1条回答
  • 2021-01-24 13:45

    I came across this question when dealing with the same problem today, so I thought I'd post my findings here.

    Using JPA native UPDATE/INSERT/DELETE queries does cause Hibernate to invalidate the entire 2nd level entity cache. As you mentioned in your question, Hibernate has a workaround for this, but it seems to not be possible to do the equivalent of Hibernate's addSynchronizedQuerySpace(), addSynchronizedEntityClass() and addSynchronizedEntityName() using pure JPA.

    What JPA however does allow you to do is to unwrap a JPA Query object to gain access to the JPA provider's API. If you're using Hibernate as a JPA provider, this will then allow you to use Hibernate's addSynchronizedXxx methods as follows:

    Query query = entityManager.createNativeQuery("UPDATE user SET ...");
    query.unwrap(org.hibernate.SQLQuery.class)
            .addSynchronizedEntityClass(User.class);
    

    It's not an ideal solution, but it will effectively allow you to prevent the entire second level cache from being invalidated.

    0 讨论(0)
提交回复
热议问题