Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-load specific entities (tables) in l2 cache with JPA

I am wondering whether it is possible to pre-load specific sets of entities (i.e. static lookup tables) into the L2 cache using generic JPA settings. Is this possible or is it JPA vendor (Hibernate, EclipseLink, etc...) dependant?

like image 732
coderatchet Avatar asked Oct 24 '25 06:10

coderatchet


2 Answers

I doubt that there's a setting for such a task. However, what seemed to work when I tried exactly that, is a simple JPQL query, which should fetch the objects into the second level cache (at least Hibernate did that):

entityManager.createQuery("select x from Entity x").getResultList();

This is plain JPA and therefore not vendor dependent and could be executed in a constructor of a corresponding DAO (@Repository or @Service in Spring) or in a prefetch thread.

like image 145
Sam Avatar answered Oct 26 '25 19:10

Sam


I wouldn't know of any such JPA feature. I'd simply trigger the respective calls (services, repositories, DAOs or whatever they are) during startup of your application.

Given the 2nd-level cache is properly setup and configured the entities representing your static lookup tables will remain in the cache until...well, that depends of course on your caching settings.

like image 35
Marcel Stör Avatar answered Oct 26 '25 19:10

Marcel Stör