How to fill cache with new values when query cache expires : Ehcache 3 and hibernate 5

I have a query cache called RatesCache to store the result of my database call of RateEntities.

I set the query cache like this for test purpose and every 30s ttl, when it expires it make a new call to database:

<cache alias="RatesCache">
    <expiry>
        <ttl unit="seconds">30</ttl>
    </expiry>
    <resources>
        <heap unit="entries">1000000</heap> 
    </resources>
    <jsr107:mbeans enable-management="true" enable-statistics="true"/>
</cache>

My rateEntity cache has a longer ttl like 12000 seconds.

My problem is whenever my query cache expires, the new result set is get BUT not stored in rateEntity cache.

The old values are still there. So when I update the database (outside of hibernate), i get the updated value when its from database each 30s but the nexts calls retrieve me the old value (the one in the entity cache) that should be replaced.

How to fix this situation, i.e clear and set the cache entity with the new result set when the ttl expires and i do the query from the database.

PS: I tried also to make the entity cache expires before but it does n+1 call issues to database.

Just clear the cache manually when you change the database through non-hibernate means. There is nothing that can be done about the N+1 problem in ORM 5. ORM 6 introduced a different cache layout that stores entity data directly in the query cache, but not sure if you can upgrade.