I am currently using hibernate's implementation of JPA to persist objects to a datasouce, successfully. Now, I would like to override hibernate's implementation of EntityManager, specifically, the createQuery function. Therefore, when my persistence context is being built, my customized EntityManager will be used instead.
Is this possible? If so, how would I proceed?
Many thanks.
EDIT:
I would like to implement my own cache. (Note: I have reviewed Hibernate's 2nd level cache and the main drawback is "caches are never aware of changes made to the persistent store by another application", so I have elected against it).
My cache would contain recently used data from the data source. Since clients requests data through the EntityManager, I would override/extend this such that the cache is checked first. If it's a miss, then the EntityManager would proceed as normal.
EDIT 2:
Alternatively, I could extend QueryImpl from org.hibernate.ejb. This would allow me to change the functionality of getResultList() and getSingleResult() to the following:
- check external cache for data
- if found, return results
- otherwise, call base class QueryImpl.getResultList (or QueryImpl.getSingleResult())