Discussion:
How do I create two EntityManager binding, one without any annotation and one with annotation?
Kohei Nozaki
2016-06-13 10:11:42 UTC
Permalink
Hello,

I want to create two EntityManager binding with guice-persist as follows:

1. Without any annotation
2. With an annotation (say, @SlaveDatabase)

So, I have created following module:

public class MyModule extends AbstractModule {

@Override
protected void configure() {
install(new MasterPrivateModule());
install(new SlavePrivateModule());
}

private static class MasterPrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("masterPU"));

expose(EntityManager.class);
}
}

private static class SlavePrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("slavePU"));

final Provider<EntityManager> entityManagerProvider =
binder().getProvider(EntityManager.class);

bind(EntityManager.class).annotatedWith(SlaveDatabase.class).toProvider(entityManagerProvider);
expose(EntityManager.class).annotatedWith(SlaveDatabase.class);
}
}
}

But it doesn't work. Guice produces following error:

com.google.inject.CreationException: Unable to create injector, see the
following errors:

1) A binding to javax.persistence.EntityManager was already configured at
mypkg.MyModule$MasterPrivateModule.configure(MyModule.java:25) (via
modules: mypkg.MyModule -> mypkg.MyModule$MasterPrivateModule).
at
com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:69)
(via modules: mypkg.MyModule -> mypkg.MyModule$SlavePrivateModule ->
com.google.inject.persist.jpa.JpaPersistModule)
...

How do I write a module to achieve this requirement?

I have pushed a reproducer (testcase) and entire project to GitHub:

* Entire project:
https://github.com/lbtc-xxx/guice-persist-multiple-pu/tree/simplified
* Reproducer (testcase):
https://github.com/lbtc-xxx/guice-persist-multiple-pu/blob/simplified/src/test/java/mypkg/MyModuleTest.java

Thanks.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/84ba2ff6-eb3e-42c0-96d1-b48e8075659e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2016-06-13 10:23:21 UTC
Permalink
This is not possible. You need to define an notation for both entitymanagers.

Also have a look at onami-persist for enhanced multi pu support
Post by Kohei Nozaki
Hello,
1. Without any annotation
public class MyModule extends AbstractModule {
@Override
protected void configure() {
install(new MasterPrivateModule());
install(new SlavePrivateModule());
}
private static class MasterPrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("masterPU"));
expose(EntityManager.class);
}
}
private static class SlavePrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("slavePU"));
final Provider<EntityManager> entityManagerProvider =
binder().getProvider(EntityManager.class);
bind(EntityManager.class).annotatedWith(SlaveDatabase.class).toProvider(entityManagerProvider);
expose(EntityManager.class).annotatedWith(SlaveDatabase.class);
}
}
}
com.google.inject.CreationException: Unable to create injector, see the
1) A binding to javax.persistence.EntityManager was already configured at
mypkg.MyModule$MasterPrivateModule.configure(MyModule.java:25) (via
modules: mypkg.MyModule -> mypkg.MyModule$MasterPrivateModule).
at
com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:69)
(via modules: mypkg.MyModule -> mypkg.MyModule$SlavePrivateModule ->
com.google.inject.persist.jpa.JpaPersistModule)
...
How do I write a module to achieve this requirement?
https://github.com/lbtc-xxx/guice-persist-multiple-pu/tree/simplified
https://github.com/lbtc-xxx/guice-persist-multiple-pu/blob/simplified/src/test/java/mypkg/MyModuleTest.java
Thanks.
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/84ba2ff6-eb3e-42c0-96d1-b48e8075659e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/EE708736-9BF3-4A7C-AFDA-853FA30726A3%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Kohei Nozaki
2016-06-19 09:20:19 UTC
Permalink
Hi Stephan,

Thanks for the info. eventually I created two annotated bindings for JPA
classes. also I have blogged about it:
http://www.nailedtothex.org/roller/kyle/entry/managing-multiple-jpa-persistence-units

Thanks,
Kohei
Post by Stephan Classen
This is not possible. You need to define an notation for both
entitymanagers.
Also have a look at onami-persist for enhanced multi pu support
Post by Kohei Nozaki
Hello,
1. Without any annotation
public class MyModule extends AbstractModule {
@Override
protected void configure() {
install(new MasterPrivateModule());
install(new SlavePrivateModule());
}
private static class MasterPrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("masterPU"));
expose(EntityManager.class);
}
}
private static class SlavePrivateModule extends PrivateModule {
@Override
protected void configure() {
install(new JpaPersistModule("slavePU"));
final Provider<EntityManager> entityManagerProvider =
binder().getProvider(EntityManager.class);
bind(EntityManager.class).annotatedWith(SlaveDatabase.class).toProvider(entityManagerProvider);
expose(EntityManager.class).annotatedWith(SlaveDatabase.class);
}
}
}
com.google.inject.CreationException: Unable to create injector, see the
1) A binding to javax.persistence.EntityManager was already configured at
mypkg.MyModule$MasterPrivateModule.configure(MyModule.java:25) (via
modules: mypkg.MyModule -> mypkg.MyModule$MasterPrivateModule).
at
com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:69)
(via modules: mypkg.MyModule -> mypkg.MyModule$SlavePrivateModule ->
com.google.inject.persist.jpa.JpaPersistModule)
...
How do I write a module to achieve this requirement?
https://github.com/lbtc-xxx/guice-persist-multiple-pu/tree/simplified
https://github.com/lbtc-xxx/guice-persist-multiple-pu/blob/simplified/src/test/java/mypkg/MyModuleTest.java
Thanks.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/415b47db-b341-439b-9ebb-32d946300b80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...