Its possible but its way more complicated.
In Fact the Provider<EntityManager>, UnitOfWork and
PersistService are all implemented in the single class
JpaPersistService.
And the JpaPersistService has a static inner class which is the
Provider<EntityManagerFactory> which it uses directly and not
over injection.
This implementation couples all the above interfaces so tightly that
I don't see a way to get in between them.
This means you would have to rewrite all of them.
It is not that hard to do. My opinion is that you would need two
classes:
- one which implements PersistService and
Provider<EntityManagerFactory>
- one which implements UnitOfWork and Provider<EntityManager>
Most of the code can be copied from the JpaPersistService.
And finally you would have to write you own XxxPersistModule which
binds the above classes.
Since creating an EntityManagerFactory is expensive it might be
worth storing the created ones in a map for later reuse.
On 09/27/2012 10:01 AM, Martin Schayna
wrote:
Thanks a lot. In my case I have to parametrize
creation of EntityManagerFactory, not EntityManager, because each
request may work with different database. Persistence unit defined
in persistence.xml is the same one, but properties like
hibernate.connection.url are passed to
Persistence.createEntityManagerFactory("persistence-unit",
myProperties).
Can I use my own Provider<EntityManager> implementation
and bind it to EntityManager interface, instead of default
implementation in guice-persist?