GWT-P + JPA Hibernate 实体管理器
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于您的“最重要的问题”,MyGuiceServletContextListener 是一个ServletContextListener。覆盖默认实现,
然后您还需要实现 contextDestroyed 来停止服务:
在这两种方法中,注入器变量在从 getInjector() 方法返回之前都在本地缓存。
Regarding your "most important question", that MyGuiceServletContextListener is a ServletContextListener. over-ride the default implementation of
then your going to want to also implement contextDestroyed to stop the serive:
In both of these methods, the injector variable is cached locally before it is returned from the getInjector() method.