仅在GWT托管模式下的Hibernate ClassCastException

时间:2011-11-16 09:32:55

标签: hibernate gwt maven jetty classcastexception

您好,感谢您的收看; - )

我们的问题/错误是:

javax.persistence.PersistenceException: [PersistenceUnit: PUname] Unable to build EntityManagerFactory
...
Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
...
Caused by: java.lang.ClassCastException: org.hibernate.dialect.MySQLDialect cannot be cast to org.hibernate.dialect.Dialect
...

(参见底部的完整堆栈跟踪)

它发生在使用以下技术的基于GWT的Web应用程序中:

  • 的Maven
  • Hibernate EntityManager / JPA
  • 吉斯
  • GWT
  • GWT-P
  • GWT Maven插件
  • ...

强调的事实是它在Tomcat中有效。 仅在开发模式(也称为托管模式)(使用Jetty),使用GWT Maven插件(使用目标gwt:run),我们得到ClassCastException。我们正在玩Hibernate依赖项而没有运气(添加hibernate-validator和/或hibernate-jpa-2.0-api)。 在实际使用任何服务之前,在JPA启动时抛出ClassCastException。 BTW:使用Guice设置JPA:

    install(new JpaPersistModule("PUname"));
    filter("/*").through(PersistFilter.class);

值得一提的是,禁用方言设置(在persistence.xml中)也会在JPA启动时创建另一个ClassCastException:

javax.persistence.PersistenceException: [PersistenceUnit: dsmgmt] Unable to build EntityManagerFactory
...
Caused by: org.hibernate.HibernateException: Unable to instantiate specified TransactionFactory class  [org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory]
...
Caused by: java.lang.ClassCastException: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory cannot be cast to org.hibernate.engine.transaction.spi.TransactionFactory
...

为什么它会在Jetty中破裂?是什么导致它或如何解决?

文件:META-INF / persistence.xml

 <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="PUname" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>our.test.class</class>
        <properties>
            <property name="hibernate.connection.username" value="user"/>
            <property name="hibernate.connection.password" value="pass"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://machine:3306/db"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
 </persistence>

我们在webapp中的JAR:

       445.288 antlr-2.7.7.jar
       150.774 antlr-runtime-3.1.3.jar
         4.467 aopalliance-1.0.jar
        57.963 classmate-0.5.4.jar
       575.389 commons-collections-3.2.1.jar
        52.543 commons-exec-1.1.jar
        59.590 commons-fileupload-1.2.2.jar
       163.151 commons-io-2.1.jar
       284.220 commons-lang-2.6.jar
       313.898 dom4j-1.6.1.jar
       131.929 dsmgmt-shared-0.1-SNAPSHOT.jar
     1.006.424 ehcache-core-2.4.3.jar
       710.492 guice-3.0.jar
        36.998 guice-assistedinject-3.0.jar
        27.633 guice-persist-3.0.jar
        65.012 guice-servlet-3.0.jar
     5.004.997 gwt-servlet-2.3.0.jar
        28.339 gwtp-dispatch-server-0.6.jar
        17.184 gwtp-dispatch-server-guice-0.6.jar
        30.402 gwtp-dispatch-shared-0.6.jar
        81.426 hibernate-commons-annotations-4.0.0.Final.jar
     4.347.499 hibernate-core-4.0.0.CR6.jar
       127.259 hibernate-ehcache-4.0.0.CR6.jar
       471.832 hibernate-entitymanager-4.0.0.CR6.jar
       102.661 hibernate-jpa-2.0-api-1.0.1.Final.jar
       366.592 hibernate-validator-4.2.0.Final.jar
        70.928 jandex-1.0.3.Final.jar
       644.148 javassist-3.12.1.GA.jar
         2.497 javax.inject-1.jar
        60.542 jboss-logging-3.1.0.CR1.jar
        11.209 jboss-transaction-api_1.1_spec-1.0.0.Final.jar
       859.016 jna-3.2.3.jar
       213.781 jsch-0.1.44-1.jar
       281.579 jsoup-1.6.1.jar
       481.535 log4j-1.2.16.jar
       789.885 mysql-connector-java-5.1.18.jar
     1.351.561 org.eclipse.jgit-1.0.0.201106090707-r.jar
       601.287 pd4ml-3.80b1.jar
       149.846 pd4ml-ss-css-3.80b1.jar
        25.962 slf4j-api-1.6.4.jar
         9.748 slf4j-log4j12-1.6.4.jar
       743.673 sqljet-1.0.4.jar
       172.023 stringtemplate-3.2.jar
     1.889.196 svnkit-1.3.5.jar
       251.371 trilead-ssh2-build213-svnkit-1.3-patch.jar
        47.433 validation-api-1.0.0.GA.jar
       109.318 xml-apis-1.0.b2.jar

完整的堆栈跟踪:

javax.persistence.PersistenceException: [PersistenceUnit: PUname] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:916)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:887)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
    at com.google.inject.persist.PersistFilter.init(PersistFilter.java:77)
    at com.google.inject.servlet.FilterDefinition.init(FilterDefinition.java:114)
    at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:98)
    at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:593)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    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:142)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:81)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:133)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2259)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2255)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:907)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:887)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
    at com.google.inject.persist.PersistFilter.init(PersistFilter.java:77)
    at com.google.inject.servlet.FilterDefinition.init(FilterDefinition.java:114)
    at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:98)
    at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:593)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    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.MySQLDialect 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:142)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:81)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:133)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2259)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2255)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:76)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:907)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:887)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
    at com.google.inject.persist.PersistFilter.init(PersistFilter.java:77)
    at com.google.inject.servlet.FilterDefinition.init(FilterDefinition.java:114)
    at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:98)
    at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:593)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    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)

祝你好运, 亚历山大

3 个答案:

答案 0 :(得分:1)

我也遇到过这个问题。 经过一番调查,我发现它是Google Eclipse Plugin + Jetty + Hibernate的类加载的特殊行为。

详细信息: 实际上在从eclipse运行或应用程序期间,libs被加载TWICE:首先加载到应用程序实例类加载器,第二次加载到jetty的web applciation类加载器。在检查类加载器层次结构时,Hibernate在加载sql方言期间有特殊的类加载观察,结果是实例不同。

解决方案:这种情况不是真正的解决方案,但我已设法通过在外部服务器上运行GWT应用程序来运行它 - 运行方式 - &gt; Web Applcaition(在外部服务器上运行)

希望有所帮助

答案 1 :(得分:0)

this patch中所述,将HHH-7084应用于org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.java为我做了(基本上用我自己的版本覆盖了该类,就像使用jetty launcher for GWT hosted mode to get the JNDI stuff to work一样)。

对于亚历山大(@ user845767)来说,回答很可能有点太晚了,但对于那些来这里寻找解决方案的人来说可能会有所帮助。

答案 2 :(得分:0)

使用hibernate 4.1.4 - 修补了这个问题 - 完全解决了这个问题