JPA Hibernate EntityManager

245 views
Skip to first unread message

Alexander Lochschmied

unread,
Nov 8, 2011, 9:37:06 AM11/8/11
to gwt-pl...@googlegroups.com

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 JpaPersistModulelike 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).

Asier

unread,
Nov 8, 2011, 10:24:26 AM11/8/11
to gwt-pl...@googlegroups.com
El 08/11/2011 15:37, Alexander Lochschmied escribió:
> 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
> |

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

Alexander Lochschmied

unread,
Nov 10, 2011, 12:23:21 PM11/10/11
to gwt-pl...@googlegroups.com
Thanks for your help Asier, it gave me some confidence.
I think my main problem was to get the correct dependencies. If it helps anyone, I use those now:


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
            <dependency>
                <groupId>com.google.inject.extensions</groupId>
                <artifactId>guice-servlet</artifactId>
            </dependency>

        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-servlet</artifactId>
        </dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-persist</artifactId>
</dependency>
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-assistedinject</artifactId>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.gwt.inject</groupId>
            <artifactId>gin</artifactId>
        </dependency>

        <dependency>
            <groupId>com.gwtplatform</groupId>
            <artifactId>gwtp-dispatch-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.gwtplatform</groupId>
            <artifactId>gwtp-dispatch-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.gwtplatform</groupId>
            <artifactId>gwtp-dispatch-server-guice</artifactId>
        </dependency>
        <dependency>
            <groupId>com.gwtplatform</groupId>
            <artifactId>gwtp-dispatch-shared</artifactId>
            <version>${gwtp.version}</version>
        </dependency>
        <dependency>
            <groupId>com.gwtplatform</groupId>
            <artifactId>gwtp-processors</artifactId>
        </dependency>
Reply all
Reply to author
Forward
0 new messages