2

我试图用 maven、gwt、gwt-platform 和 guice-persist 做一个例子。

但是,我遇到了麻烦。

当我尝试使用 运行示例时mvn gwt:run,出现以下异常:

1) Error injecting constructor, javax.persistence.PersistenceException: [PersistenceUnit: exemplo] Unable to build EntityManagerFactory   at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:9)   while locating br.net.meditec.Exemplo.server.inject.AppInitializer  1 error    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)     at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)  at br.net.meditec.Exemplo.server.inject.GuiceServletConfig.getInjector(GuiceServletConfig.java:24)  at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)   at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)   at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)  at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.Server.doStart(Server.java:222)    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)   at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)     at com.google.gwt.dev.DevMode.main(DevMode.java:311) Caused by: javax.persistence.PersistenceException: [PersistenceUnit: exemplo] Unable to build EntityManagerFactory     at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)     at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:10)   at br.net.meditec.Exemplo.server.inject.AppInitializer$$FastClassByGuice$$96bdac91.newInstance(<generated>)     at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)   at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)   at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)  at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)     at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)  at br.net.meditec.Exemplo.server.inject.GuiceServletConfig.getInjector(GuiceServletConfig.java:24)  at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)   at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)   at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)  at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.Server.doStart(Server.java:222)    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)   at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)     at com.google.gwt.dev.DevMode.main(DevMode.java:311) Caused by: org.hibernate.HibernateException: Could not instantiate dialect class   at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:82)  at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:64)  at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:146)     at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)   at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)  at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)     at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)   at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)     at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:88)  at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)     at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:10)   at br.net.meditec.Exemplo.server.inject.AppInitializer$$FastClassByGuice$$96bdac91.newInstance(<generated>)     at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)   at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)   at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)  at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)     at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)  at br.net.meditec.Exemplo.server.inject.GuiceServletConfig.getInjector(GuiceServletConfig.java:24)  at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)   at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)   at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)  at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.Server.doStart(Server.java:222)    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)   at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)     at com.google.gwt.dev.DevMode.main(DevMode.java:311) Caused by: java.lang.ClassCastException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to org.hibernate.dialect.Dialect   at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:73)  at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:64)  at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:146)     at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)   at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)  at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)     at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)   at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)     at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:88)  at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)  at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)     at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:10)   at br.net.meditec.Exemplo.server.inject.AppInitializer$$FastClassByGuice$$96bdac91.newInstance(<generated>)     at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)   at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)   at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)  at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)     at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)  at br.net.meditec.Exemplo.server.inject.GuiceServletConfig.getInjector(GuiceServletConfig.java:24)  at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)   at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)   at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)  at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)    at org.mortbay.jetty.Server.doStart(Server.java:222)    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)     at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)   at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)     at com.google.gwt.dev.DevMode.main(DevMode.java:311)

如果我通过 eclipse 运行模块,我会遇到另一个异常:

1) Error injecting constructor, javax.persistence.PersistenceException: No Persistence provider for EntityManager named exemplo
  at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:9)
  while locating br.net.meditec.Exemplo.server.inject.AppInitializer

1 error
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
    at br.net.meditec.Exemplo.server.inject.GuiceServletConfig.getInjector(GuiceServletConfig.java:24)
    at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:45)
    at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:468)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)
    at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
    at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)
    at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)
    at com.google.gwt.dev.DevMode.main(DevMode.java:311)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named exemplo
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
    at br.net.meditec.Exemplo.server.inject.AppInitializer.<init>(AppInitializer.java:10)
    at br.net.meditec.Exemplo.server.inject.AppInitializer$$FastClassByGuice$$96bdac91.newInstance(<generated>)
    at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40)
    at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978)
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974)
    ... 21 more

在第一个错误中,似乎没有找到 PostgreSQLDialect,在第二个错误中,似乎没有找到 persistence.xml 本身。

我创建了一个包含以下内容的类,它工作得很好:

public class DaoInjectionTest {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new DaoModule(),
                new JpaPersistModule("exemplo"));
        injector.getInstance(AppInitializer.class);

        injector.getInstance(RacaDao.class).save(new Raca("cachorro pastor alemao"));
    }
}

所以,我无法理解发生了什么。有人可以帮忙吗?

如果有帮助,我的项目代码在 github 中

非常感谢。

4

0 回答 0