2012-01-17 81 views
9

我有一個GWT + Hibernate + JPA + Spring配置文件,這與Spring 3.0.x + Hibernate 3.6.x一致。當我升級到Spring 3.1和Hibernate 4.0時,拋出以下異常:在GWT開發模式+春季3.1 +休眠4.0.1的異常

是否有任何已知的與Hibernate 4和Spring 3.1不兼容?

 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/application-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: myPu] Unable to build EntityManagerFactory 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
    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: naftPu] 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.createContainerEntityManagerFactory(HibernatePersistence.java:73) 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
    ... 30 more 
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:187) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:234) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:91) 
    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:2270) 
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735) 
    at org.hibernate.ejb.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:84) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) 
    ... 36 more 
Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider] 
    at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:190) 
    at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:112) 
    at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:54) 
    at org.hibernate.service.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:69) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:177) 
    ... 49 more 
Caused by: java.lang.ClassCastException: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider 
    at org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:187) 
    ... 53 more 

這裏是我的應用程序的context.xml配置的一部分:

<bean id="mainDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> 
    <property name="url" value="***manually deleted***" /> 
    <property name="username" value="myschema" /> 
    <property name="password" value="myschema" /> 
    <property name="initialSize" value="1" /> 
    <property name="maxActive" value="4" /> 
    <property name="maxIdle" value="-1" /> 
    <property name="maxWait" value="10000" /> 
</bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:persistenceUnitName="myPu"> 
    <property name="dataSource" ref="mainDataSource" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true" 
      p:database="ORACLE" p:databasePlatform="org.hibernate.dialect.Oracle10gDialect" /> 
    </property> 
    <property name="jpaPropertyMap"> 
     <map merge="true"> 
      <entry key="hibernate.archive.autodetection" value="class" /> 
      <entry key="hibernate.format_sql" value="false" /> 
      <entry key="hibernate.hbm2ddl.auto" value="false" /> 
      <entry key="hibernate.default_schema" value="myschema" /> 
     </map> 
    </property> 
</bean> 

的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> 
    <persistence-unit name="myPu" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    </persistence-unit> 
</persistence> 
+0

粘貼mainDataSource豆配置和persistence.xml中:

對於org.hibernate.service.classloading.internalClassLoaderServiceImpl通過構造方法設置父爲NULL。你的服務器是什麼?你完全使用EJB嗎?這個類很奇怪:org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider。 我發現: http://relation.to/Bloggers/SomeTipsOnUsingHibernateInJBossAS700Final – 2012-01-17 08:25:21

+0

它看起來像這樣:org.hibernate.ejb.HibernatePersistence是錯誤的提供者。 – 2012-01-17 08:30:24

+0

@ peter-gwiazda謝謝,那個頁面沒有幫助,因爲我在JBoss之外。順便說一下,除了org.hibernate.ejb.HibernatePersistence之外,沒有其他的Hibernate的JPA提供者。 – Mohsen 2012-01-17 08:34:14

回答

6

此問題已修復in Hibernate 4.1.4


我有同樣的問題,而且看起來(GWT的插件使用碼頭託管模式),以進行相關的碼頭類加載器,GWT的插件和Hibernate(雖然它顯然與其他圖書館作物行動,以及)。

它與此處提到的錯誤Hibernate Bug Report類似,但在我們的案例中,我們通過GWT插件運行它,並導致ConnectionProvider出現ClassCastException。

錯誤報告中提到的修復應該修復該特定問題。

this.classClassLoader = new ClassLoader(null) { 
     @Override 
     protected Class<?> findClass(String name) throws ClassNotFoundException { 
      for (ClassLoader loader : classLoadingClassLoaders) { 
       try { 
        return loader.loadClass(name); 
       } 
       catch (Exception ignore) { 
       } 
      } 
      throw new ClassNotFoundException("Could not load requested class : " + name); 
     } 
}; 
+1

這在hibernate 4.1.4中修復 – gabuzo 2012-06-01 08:04:00

-2

我覺得你並不需要設置<provider>org.hibernate.ejb.HibernatePersistence</provider>persistence.xml。檢查the docs

+0

沒有變化。此元素有助於JPA在類路徑中存在多個提供程序時決定選擇哪個提供程序。 – Mohsen 2012-01-17 08:38:48

1

如果已經存在,則刪除/ lib目錄中的ejb3-persistence.jar;它與hibernate 4中的hibernate-jpa-2.0-api-1.0.1.Final.jar衝突。這個問題對我有用,我在Openshift環境中實現了一個GWT,Hibernate 4,Spring 3.1.1應用程序https://openshift.redhat.com/app/