问题
Hello and thanks for watching ;-)
Our problem/error is:
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
...
(see full stack trace at the bottom)
It occurs in a GWT based web application using the following technologies:
- Maven
- Hibernate EntityManager /JPA
- Guice
- GWT
- GWT-P
- GWT Maven Plugin
- ...
The fact to stress is that it does work in Tomcat.
Only in development mode aka hosted mode (with Jetty), using GWT Maven Plugin (with goal gwt:run), we get the ClassCastException. We were playing with Hibernate dependencies without luck (adding hibernate-validator
and/or hibernate-jpa-2.0-api
).
The ClassCastException is thrown on startup of JPA before any service is actually used.
BTW: JPA is setup using Guice:
install(new JpaPersistModule("PUname"));
filter("/*").through(PersistFilter.class);
It may be worth mentioning that disabling the dialect setting (in persistence.xml) creates another ClassCastException also on JPA startup:
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
...
Why does it break in Jetty? What could be causing it or how can it be fixed?
File: 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>
Our JARs in the webapp:
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
The full stacktrace:
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)
Best regards, Alexander
回答1:
I've also faced with that problem. After some investigation, i've found out that it is special behavior of classloading of Google Eclipse Plugin + Jetty + Hibernate.
Details: In fact during running or application from eclipse the libs are loaded TWICE: first to application instance classloader and second time to web applciation classloader of jetty. Hibernate has special classloading observing during loading of sql dialects in checking classloader hierarchy and as result that instances are different.
Solution: Not real solution of this case, but i've managed to handle it with debuging GWT applcation on external server Run As -> Web Applcaition (run on external server)
Hope it helps
回答2:
Applying this patch to org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.java
as described in HHH-7084 did it for me (basically overriding that class with my own version just like with the jetty launcher for GWT hosted mode to get the JNDI stuff to work).
The answer is very likely a little too late to be of value for Alexander (@user845767) but may be helpful for someone else who comes here looking for a solution.
回答3:
using hibernate 4.1.4 - which has this problem patched - solved the issue completely
来源:https://stackoverflow.com/questions/8149401/hibernate-classcastexception-in-gwt-hosted-mode-only