【问题标题】:Spring Data JPA persistence xml config: hibernateJpaVendorAdaptor propertySpring Data JPA 持久性 xml 配置:hibernateJpaVendorAdaptor 属性
【发布时间】:2015-05-27 19:26:26
【问题描述】:

我的 Spring 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)

【问题讨论】:

  • 检查您是否在配置中包含了必要的日志记录 jar。例如,log4j
  • 是的,确实需要 jboss 日志记录 jar。我仍然看到类似的错误,发生在 emf initi 期间:java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object;跨度>
  • 可能你弄乱了库的版本,检查你有什么日志 jar 版本,以及为你带来 HibernateJpaVendorAdapter 的库需要什么

标签: xml spring hibernate jpa persistence


【解决方案1】:

试试这个文件..希望我编辑了所有内容...

            <?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2013-09-30
    • 2013-10-31
    相关资源
    最近更新 更多