EntityProxy injection with Guice - Guice-persist

68 views
Skip to first unread message

koma

unread,
Sep 23, 2011, 11:12:22 AM9/23/11
to google-we...@googlegroups.com
Hi

Starting up a new project using RF and guice-persist on the server-side to start JPA and provide transaction management.

I have a couple of tests going to inject the EntityManager into the services via the ServiceLocator :

public class ApplicationServiceLocator implements ServiceLocator {
Injector injector = null;
public ApplicationServiceLocator() {
injector = Guice.createInjector(new JpaPersistModule("Test"));
injector.getInstance(ApplicationInitializer.class);
}
@Override
public Object getInstance(Class<?> clazz) {
return injector.getInstance(clazz);
}
}

This makes sure that is EntityManager injected into the service layers :

public class WorkerServiceImpl {

@Inject
private EntityManager em;

@Transactional
public List<Contract> getContracts(User user) {
// I can us EntityManager now !! Injected because created this WorkerServiceImpl was created via injector.getInstance
 }

My question : what would be a good approach to inject the EntityManager into an entity class when implementing a service in an entity class ?
What hooks do I have available to do so ?


thx 

Koen

Magno Machado

unread,
Sep 23, 2011, 3:45:40 PM9/23/11
to google-we...@googlegroups.com
>@Inject
>private EntityManager em;
Don't know how to answer your question, but here you should inject Provider<EntityManager> instead of EntityManager. The reason is that service classes are stored like singletons, and hence you should inject a provider as explained here http://code.google.com/p/google-guice/wiki/JPA#Using_the_inside_transactions

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/CDzbDvioxCAJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



--
Magno Machado Paulo
http://blog.magnomachado.com.br
http://code.google.com/p/emballo/

koma

unread,
Sep 27, 2011, 11:04:47 AM9/27/11
to google-we...@googlegroups.com
Ok, thx  I figured it out...

First I needed to inject a ServiceLayerDecorator to with Guice like this example project https://github.com/mgenov/injecting-request-factory
And indeed, the EntityManager must be passed as Provider<EntityManager> otherwise it is not ThreadLocal and you run into closed EntityManager exception (because the trheads make concurrent use of the EntityManager).

I got it working.

Reply all
Reply to author
Forward
0 new messages