Spring Data JPA持久化xml config:hibernateJpaVendorAdaptor属性

时间:2015-05-27 19:26:27

标签: xml spring hibernate jpa persistence

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

1 个答案:

答案 0 :(得分: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>