配置Spring JPA:无法实例化[org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]

时间:2018-03-12 03:46:53

标签: spring jpa spring-data spring-data-jpa

我正在尝试配置 Spring Data JPA 项目,配置如下:

的applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- Using Spring annotations -->
    <tx:annotation-driven />

    <!-- Activate scanning of @Autowired -->
    <context:annotation-config />

    <!-- Activate scanning of @Repository -->
    <context:component-scan base-package="boo.com" />

    <!-- Active scanning Repositories -->
    <jpa:repositories base-package="boo.com.repo"></jpa:repositories>

    <!-- Load JDBC properties -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:jdbc.properties" />

    <!-- Configure C3P0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close" p:driverClass="${jdbc.driverClassName}"
        p:jdbcUrl="${jdbc.databaseurl}" p:user="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    </bean>

    <!-- Not sure if it's needed -->
    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="defaultDataSource" ref="dataSource" />
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <!-- Configure EntityManagerFactory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <!-- Scannig model -->
        <property name="packagesToScan">
            <list>
                <value>boo.com.model</value>
            </list>
        </property>

        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
            </props>
        </property>

    </bean>

    <!-- Configure transaction manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
    </bean>

</beans>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>CRUD-SpringData</groupId>
    <artifactId>CRUD-SpringData</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.2.14.Final</version>
        </dependency>

    </dependencies>

</project>

当我尝试部署项目时,它会抛出一个错误:

  

线程中的异常&#34; main&#34;   org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory&#39;的bean在类路径中定义   resource [applicationContext.xml]:无法解析对bean的引用   &#39; jpaVendorAdapter&#39;同时设置bean属性&#39; jpaVendorAdapter&#39 ;;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; jpaVendorAdapter&#39;的bean在类路径中定义   resource [applicationContext.xml]:bean的实例化失败;   嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例   [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:   构造函数抛出异常;嵌套异常是   java.lang.NoClassDefFoundError:org / hibernate / HibernateException at   org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)     在   org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在   org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)     在   org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:140)     在   org.springframework.context.support.FileSystemXmlApplicationContext。(FileSystemXmlApplicationContext.java:84)     在Test.main(Test.java:16)引起:   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; jpaVendorAdapter&#39;的bean在类路径中定义   resource [applicationContext.xml]:bean的实例化失败;   嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例   [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:   构造函数抛出异常;嵌套异常是   java.lang.NoClassDefFoundError:org / hibernate / HibernateException at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在   org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)     ... 15更多引起:   org.springframework.beans.BeanInstantiationException:失败   实例   [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:   构造函数抛出异常;嵌套异常是   java.lang.NoClassDefFoundError:org / hibernate / HibernateException at   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)     在   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)     ... 23更多引起:java.lang.NoClassDefFoundError:   org / hibernate / HibernateException at   org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter。(HibernateJpaVendorAdapter.java:66)     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   方法)at   sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown   来自java.lang.reflect.Constructor.newInstance(未知来源)     在   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)     ... 25更多引起:java.lang.ClassNotFoundException:   org.hibernate.HibernateException at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)... 31更多

有谁可以请教我错过了什么?我真的很感激。

[更新

我尝试更新项目如下:

  • 添加hibernate核心依赖
  • 将platform-b​​om版本从Brussels-SR7更改为Brussels-SR6
  • 将C3p0更改为JDBC

所有这些更改都不起作用并继续抛出相同的错误。

根本原因 该错误来自maven依赖项。我删除了 ./ m2 并重建了该项目。它现在工作正常。不知道如何在项目中发现这类问题。

1 个答案:

答案 0 :(得分:2)

如果查看stackTrace(java.lang.ClassNotFoundException:org.hibernate.HibernateException),则表明它无法加载HibernateException。 Class属于hibernate-core依赖。

请在pom.xml中包含以下内容

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

这可以解决您的问题。