GWT-P + JPA Hibernate 实体管理器

发布于 2024-12-14 07:18:38 字数 1590 浏览 2 评论 0原文

我正在尝试在 GWT-P 应用程序中使用 Hibernate 的 EntityManager 。

不幸的是,看起来我无法使用建议的 PersistFilter

public class MyModule extends ServletModule {
  protected void configureServlets() {
    install(new JpaPersistModule("myJpaUnit"));  // like we saw earlier.
    filter("/*").through(PersistFilter.class);
  }
}

它会导致 ClassCastException

org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider

所以我正在尝试其他方法(除非您对此有建议)。

我必须非常接近让第一个服务正常工作,但注入的 EntityManager 始终为 null

public class ImageMetaDataService {
    @Inject EntityManager em;
    @Transactional
    public void createNewImageMetaData(ImageMetaDataImpl imd) {
        em.persist(imd);
    }
}

我怀疑我在设置中犯了错误。 )与添加 JpaPersistModule 有何区别:

public class MyGuiceServletContextListener extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServerModule(), new DispatchServletModule(), new JpaPersistModule("name"));
    }
}

使用 install(new JpaPersistModule("name")); (在 DispatchServletModule中 我最重要的问题:我将如何开始 JPA。文档建议这样的类:

public class MyInitializer { 
        @Inject MyInitializer(PersistService service) {
                service.start(); 
                 // At this point JPA is started and ready.
        } 
}

但我不知道如何做到这一点(在 GWT-P 中)。

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("myJpaUnit"));  // like we saw earlier.
    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 (in GWT-P).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-12-21 07:18:38

关于您的“最重要的问题”,MyGuiceServletContextListener 是一个ServletContextListener。覆盖默认实现,

public void contextInitialized(ServletContextEvent servletContextEvent) {
        persistService = injector.getInstance(PersistService.class);
        persistService.start();
}

然后您还需要实现 contextDestroyed 来停止服务:

@Override
public void contextDestroyed(ServletContextEvent contextEvent) {
    if (persistService != null) {
        persistService.stop();
    }
}

在这两种方法中,注入器变量在从 getInjector() 方法返回之前都在本地缓存。

Regarding your "most important question", that MyGuiceServletContextListener is a ServletContextListener. over-ride the default implementation of

public void contextInitialized(ServletContextEvent servletContextEvent) {
        persistService = injector.getInstance(PersistService.class);
        persistService.start();
}

then your going to want to also implement contextDestroyed to stop the serive:

@Override
public void contextDestroyed(ServletContextEvent contextEvent) {
    if (persistService != null) {
        persistService.stop();
    }
}

In both of these methods, the injector variable is cached locally before it is returned from the getInjector() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文