2015-05-27 78 views
0

我的Spring xml配置出現錯誤。已嘗試以兩種不同的方式設置hibernateJpaVendorAdaptor屬性,並且都給我錯誤,請參見下文。有關如何解決的任何想法?Spring數據JPA持久性xml配置:hibernateJpaVendorAdaptor屬性

第一次嘗試

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/data/jpa 
          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/data/repository 
          http://www.springframework.org/schema/data/repository/spring-repository.xsd"> 

    <context:property-placeholder location="classpath:application.properties"/> 

    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"> 
     <property name="driverClass" value="${db.driver}"/> 
     <property name="jdbcUrl" value="${db.url}"/> 
     <property name="username" value="${db.username}"/> 
     <property name="password" value="${db.password}"/> 
    </bean> 


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <tx:annotation-driven/> 

    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> 
     <property name="packagesToScan" value="com.metagravy.ark"/> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <jpa:repositories base-package="com.metagravy.ark.security"/> 


</beans> 

錯誤

Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringDbContext.xml]: Cannot resolve reference to bean 'HibernateJpaVendorAdapter' while setting bean property 'jpaVendorAdapter'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'HibernateJpaVendorAdapter' is defined 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) 

第二次嘗試

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/data/jpa 
          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/data/repository 
          http://www.springframework.org/schema/data/repository/spring-repository.xsd"> 

    <context:property-placeholder location="classpath:application.properties"/> 

    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"> 
     <property name="driverClass" value="${db.driver}"/> 
     <property name="jdbcUrl" value="${db.url}"/> 
     <property name="username" value="${db.username}"/> 
     <property name="password" value="${db.password}"/> 
    </bean> 


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <tx:annotation-driven/> 

    <!-- <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> --> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <!-- <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> --> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="packagesToScan" value="com.metagravy.ark"/> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <jpa:repositories base-package="com.metagravy.ark.security"/> 


</beans> 

錯誤

Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringDbContext.xml]: Cannot create inner bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#155d2eb4' of type [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter] while setting bean property 'jpaVendorAdapter'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#155d2eb4' defined in class path resource [SpringDbContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/jboss/logging/Logger 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313) 
+0

檢查是否包含在你的配置必要的記錄罐子。例如,log4j – codedabbler

+0

是的,確實需要jboss日誌記錄罐。我仍然看到類似的錯誤,雖然發生在emf啓動過程中:java.lang.NoSuchMethodError:org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class; Ljava/lang/String;)Ljava/lang/Object; –

+0

可能你用libs的版本搞砸了一些東西,檢查你有什麼日誌jar版本,以及lib給你帶來什麼需求HibernateJpaVendorAdapter – Nadir

回答

0

嘗試用這個文件..希望我編輯的一切......

  <?xml version="1.0" encoding="UTF-8"?> 
      <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans.xsd 
             http://www.springframework.org/schema/data/jpa 
             http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx.xsd 
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context.xsd 
             http://www.springframework.org/schema/data/repository 
             http://www.springframework.org/schema/data/repository/spring-repository.xsd"> 

       <context:property-placeholder location="classpath:application.properties"/> 

       <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"> 
        <property name="driverClass" value="${db.driver}"/> 
        <property name="jdbcUrl" value="${db.url}"/> 
        <property name="username" value="${db.username}"/> 
        <property name="password" value="${db.password}"/> 
       </bean> 

       <!-- Activate Spring Data JPA repository support --> 
       <jpa:repositories base-package="your.package.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/> 


       <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
        <property name="entityManagerFactory" ref="entityManagerFactory"/> 
       </bean> 

       <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 

       <util:properties id="hibernatePropertiesProps"> 
        <!--<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> --> 
        <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
        <prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop> 
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 

       </util:properties> 


       <tx:annotation-driven/> 

       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
         p:persistenceProviderClass="org.hibernate.ejb.HibernatePersistence" 
         p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter" 
         p:jpaProperties-ref="hibernatePropertiesProps" 
         p:dataSource-ref="dataSource" 
         p:packagesToScan= "com.metagravy.ark" > 
        </bean> 


      <!-- 
       <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
        <property name="dataSource" ref="dataSource"/> 

        <property name="jpaVendorAdapter"> 
         // should have been 
         <ref bean="hibernateJpaVendorAdapter"/> 
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
        </property> 
        <property name="packagesToScan" value="com.metagravy.ark"/> 
        <property name="jpaProperties"> 
         <props> 
          <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
          <prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop> 
          <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
          <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
          <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
         </props> 
        </property> 
       </bean> 

       --> 
       <jpa:repositories base-package="com.metagravy.ark.security"/> 


      </beans>