Guice Persist: How Best to Hook Into EntityManagerFactory Creation
- Dominant language
- Java
- Stars
- 12.7k
- Forks
- 1.7k
- Avg merge
- 11m
- Merged PRs (30d)
- 2
Description
We are trying to use Guice Persist in our project. However, our project requires us to create EntityManagerFactories on the fly during runtime.
Sadly, Guice Persist expects all JPA persistence units to be known and defined ahead of time and installed via the `JpaPersistModule`.
Looking at the source code for Guice Persist, it doesn't seem like there's any way to provide our own EntityManagerFactory provider or even EntityManager provider.
`JpaPersistModule` is **final** meaning we can't override it to define our own bindings for EntityManagerFactory, EntityManger, etc.
Here are the lines of code in the "sealed" `JpaPersistModule` that we would like to override or extend:
``` java
@Override protected void configurePersistence() {
bindConstant().annotatedWith(Jpa.class).to(jpaUnit);
bind(JpaPersistService.class).in(Singleton.class);
bind(PersistService.class).to(JpaPersistService.class);
bind(UnitOfWork.class).to(JpaPersistService.class);
bind(EntityManager.class).toProvider(JpaPersistService.class);
bind(EntityManagerFactory.class)
.toProvider(JpaPersistService.EntityManagerFactoryProvider.class);
transactionInterceptor = new JpaLocalTxnInterceptor();
requestInjection(transactionInterceptor);
// Bind dynamic finders.
for (Class finder : dynamicFinders) {
bindFinder(finder);
}
}
```
I believe we could extend the `PersistModule` but in addition to configure all the persistence bindings, it also requires derived classes to implement:
``` java
protected abstract MethodInterceptor getTransactionInterceptor();
```
Ideally we would like to use the existing `JpaLocalTxnInterceptor` but that class is marked package-private.
So it appears we literally need to re-write the entire Guice-Persist "framework" when we'd really just like to override the binding for EntityManagerFactory shown above:
``` java
bind(EntityManagerFactory.class)
.toProvider(JpaPersistService.EntityManagerFactoryProvider.class);
```
Any thoughts or ideas on how best to do this without "re-creating" the wheel?
Contributor guide
Assessment
This issue has not been assessed yet.