Injecting an EntityManager specifying the persistence unit with @javax.inject.Qualifier

367 views
Skip to first unread message

Robério Cardoso Fernandes

unread,
Sep 28, 2016, 11:20:16 AM9/28/16
to google-guice
Hi folks,
I'm doing the unit tests in my project, and I'm trying to use the Google Guice. My repository classes have your EntityManagers, and, in my project, to specify the persistence unit I have an annotation:

@javax.inject.Qualifier
@java.lang.annotation.Retention(RUNTIME)
@java.lang.annotation.Target({FIELD, TYPE, METHOD})
public @interface ConnectionDatabase {
}


In my repositories the entity managers are injected like this:

public class MyRepository implements IMyRepository {

    @javax.inject.Inject
    @ConnectionDatabase
    private javax.persistence.EntityManager entityManager;
}


M ResourceProducer, like this:

public class ResourceProducer {

    @javax.enterprise.inject.Produces
    @Inject
    @ConnectionDatabase
    private javax.persistence.EntityManager entityManager;
}


My Module of Google Guice is like this:

public class ModuleTest extends AbstractModule {

    @Override
    public void configure() {
        bind(new TypeLiteral<IMyRepository>() {}).to(MyRepository.class);
    }
}


I have a persistence.xml with a persistence unit called "TestPU".


In my unit tests (or cintegration tests), using the Google Guice, the EntityManager is not injected, and the following error is occurring:
No implementation for javax.persistence.EntityManager annotated with @ConnectionDatabase() was bound.
  while locating javax.persistence.EntityManager annotated with @ConnectionDatabase()
    for field at MyRepository.entityManager(<the line>)
  at ModuloTest.configure(<the line of the bind "bind(new TypeLiteral<IMyRepository>() {}).to(MyRepository.class)">)


Someone here can help me, please?

Stephan Classen

unread,
Sep 28, 2016, 1:12:46 PM9/28/16
to google...@googlegroups.com
Where do you bind the EntityManager? Is it bound just in time? At runtime do you have a container or similar which provides the EntityManager?

Robério Cardoso Fernandes

unread,
Sep 28, 2016, 1:36:28 PM9/28/16
to google-guice
I'm doing the integration tests with Apache OpenEJB (tomee). The bind is done when the container is started.

Stephan Classen

unread,
Sep 28, 2016, 6:47:57 PM9/28/16
to google...@googlegroups.com
I am not 100% sure but I think guice does not support the @Produces annotation.
So far I only used @Provides

Also if you are using tomEE you already have a CDI container. What is your reason to use guice in such an environment.

Don't missunderstand me. I prefer guice over CDI but why chose tomEE and then don't use its feature...

Robério Cardoso Fernandes

unread,
Sep 29, 2016, 12:51:30 PM9/29/16
to google-guice
My project have some maven modules, one of that is the repository module, an other is the web module.
In repository module I'm using the google guice to do the dependency injection, and any more.
In web module I'm using the tomEE, and that module have all others (api, services and repositories).
My problem was in repository module, when I would inject the EntityManager object in repositories, to solve this problem I put in the implementation of Guice AbstractModule it:
bind(EntityManager.class).annotatedWith(ConnectionDatabase.class).to(EntityManager.class);

What do you thing about?


Thank you for the attention!

Stephan Classen

unread,
Sep 29, 2016, 3:31:27 PM9/29/16
to google...@googlegroups.com

The binding

bind(EntityManager.class).annotatedWith(ConnectionDatabase.class).to(EntityManager.class);

tells guice to use the default binding of defined for EntityManager. So there must be somewhere a binding or a provider method which defines how to get an implementation of the EntityManager interface.

If you need JPA with guice have a look at onami-persist.

--
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/eeb80f3e-2d84-429a-9a08-721dd314b72b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages