3

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:

  1. check external cache for data
  2. if found, return results
  3. otherwise, call base class QueryImpl.getResultList (or QueryImpl.getSingleResult())
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
t.smith.htc
  • 203
  • 1
  • 3
  • 12
  • 1
    Can you expand on what you need to do? Hibernate offers a lot of customisation options without having to override the EM. – Alan Hay Nov 13 '13 at 20:18
  • Original post has bee edited. – t.smith.htc Nov 13 '13 at 21:08
  • The behavior you want to implement is the normal behavior of any cache. Just use any of the existing 2nd-level cache implementations, and they will all do what you describe. – JB Nizet Nov 13 '13 at 21:33
  • The main reason I do not use existing 2nd level cache is because I can build my own cache externally, that will be able to handle updates from other applications without needing to expire data at fixed regular intervals. As is the case with Hibernate's 2nd level cache: "caches are never aware of changes made to the persistent store by another application". – t.smith.htc Nov 14 '13 at 14:08

0 Answers0