I'm trying to use Hibernate's EntityManager
in a GWT-P application.
Unfortunately it looks like I cannot use the proposed PersistFilter
public class MyModule extends ServletModule {
protected void configureServlets() {
install(new JpaPersistModule("name"));
filter("/*").through(PersistFilter.class);
}
}
it causes ClassCastException
:
org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider
So I'm trying other approach (unless you have a suggestion for this one).
I must be pretty close to get a first service to work, but the injected EntityManager
is always null
public class ImageMetaDataService {
@Inject EntityManager em;
@Transactional
public void createNewImageMetaData(ImageMetaDataImpl imd) {
em.persist(imd);
}
}
I suspect I make a mistake in setup. Is there a difference in using install(new JpaPersistModule("name"));
(in DispatchServletModule
) versus adding JpaPersistModule
like this?
public class MyGuiceServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServerModule(), new DispatchServletModule(), new JpaPersistModule("name"));
}
}
And finally my most important question: How would I start JPA? Documentation suggest a class like this:
public class MyInitializer {
@Inject MyInitializer(PersistService service) {
service.start();
// At this point JPA is started and ready.
}
}
But I don't see how to do that or "call it" (in GWT-P).
Perhaps you should move the the install line below the filter one.
C3P0 connection error can be related to your persistence.xml file.
> So I'm trying other approach (unless you have a suggestion for this one).
>
> I must be pretty close to get a first service to work, but the injected
> |EntityManager| is always |null|
>
> |public class ImageMetaDataService {
> @Inject EntityManager em;
> @Transactional
> public void createNewImageMetaData(ImageMetaDataImpl imd) {
> em.persist(imd);
> }
> }
> |
Perhaps you haven't binded ImageMetaDataService class in your ClientModule?
If your service is a Singleton then you should inject a Provicer<EntityManager>
@Inject
protected com.google.inject.Provider<EntityManager> emp;
@Override
public T find(PK id) {
return this.emp.get().find(this.persistedClass, id);
}
> I suspect I make a mistake in setup. Is there a difference in using
> |install(new JpaPersistModule("name"));| (in |DispatchServletModule|) versus
> adding |JpaPersistModule|like this?
>
> |public class MyGuiceServletContextListener extends GuiceServletContextListener {
> @Override
> protected Injector getInjector() {
> return Guice.createInjector(new ServerModule(), new DispatchServletModule(), new JpaPersistModule("name"));
> }
> }
> |
I have this in my DispatchServletModule class and works fine.
@Override
public void configureServlets() {
this.bindConstants();
this.bindFilters();
this.bindServlets();
}
private void bindConstants() {
this.bindConstant().annotatedWith(SecurityCookie.class)
.to(SharedTokens.securityCookieName);
}
private void bindFilters() {
// Security cookie
this.filter("*").through(HttpSessionSecurityCookieFilter.class);
// Persistence and finders
this.filter("/*").through(PersistFilter.class);
this.install(new JpaPersistModule("ofertas-backend")
.addFinder(ParameterRepository.Accessor.class));
}
> And finally my most important question: How would I start JPA? Documentation
> suggest a class like this:
>
> |public class MyInitializer {
> @Inject MyInitializer(PersistService service) {
> service.start();
> // At this point JPA is started and ready.
> }
> }
> |
>
> But I don't see how to do that or "call it" (in GWT-P).
This is not needed in a servlet container. Guice-persist initializes itself
the persistence service.
I use that clas in my unit tests to initialize the persistence service via
injector.getInstance(MyInitializer.class);
Regards