gpt4 book ai didi

仅在 GWT 托管模式下的 Hibernate ClassCastException

转载 作者:行者123 更新时间:2023-12-01 18:15:13 26 4
gpt4 key购买 nike

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

我们的问题/错误是:

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。顺便说一句:JPA 是使用 Guice 设置的:

    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>

我们在网络应用程序中的 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)

最诚挚的问候,亚历山大

最佳答案

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

详细信息:事实上,在 Eclipse 运行或应用程序期间,库会加载两次:第一次加载到应用程序实例类加载器,第二次加载到 jetty 的 Web 应用程序类加载器。 Hibernate 在加载 sql 方言期间有特殊的类加载观察,以检查类加载器层次结构,从而导致实例不同。

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

希望对你有帮助

关于仅在 GWT 托管模式下的 Hibernate ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8149401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com