首页 > 解决方案 > 将 spring-data-jpa 的问题从 1.9.5.RELEASE 升级到 2.1.2.RELEASE

问题描述

我正在尝试将 spring-data-jpa 升级到其最新版本,即 2.1.2.RELEASE。但是我在tomcat中部署项目时遇到了一个问题。

[INFO ] 2018-11-14 13:07:52.454 [localhost-startStop-1] LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
[WARN ] 2018-11-14 13:07:53.317 [localhost-startStop-1] XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountRestController': Unsatisfied dependency expressed through field 'pgTransactionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'PGTransactionServiceImpl': Unsatisfied dependency expressed through field 'onlineTxnLogDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'onlineTxnLogDao': Unsatisfied dependency expressed through field 'onlineTxnLogRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'onlineTxnLogRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.jpa.repository.support.QuerydslJpaPredicateExecutor]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.chat.dao.model.QPGOnlineTxnLog cannot be cast to com.querydsl.core.types.EntityPath
[INFO ] 2018-11-14 13:07:53.318 [localhost-startStop-1] LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
[ERROR] 2018-11-14 13:07:53.332 [localhost-startStop-1] ContextLoader - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountRestController': Unsatisfied dependency expressed through field 'pgTransactionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'PGTransactionServiceImpl': Unsatisfied dependency expressed through field 'onlineTxnLogDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'onlineTxnLogDao': Unsatisfied dependency expressed through field 'onlineTxnLogRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'onlineTxnLogRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.jpa.repository.support.QuerydslJpaPredicateExecutor]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.chat.dao.model.QPGOnlineTxnLog cannot be cast to com.querydsl.core.types.EntityPath
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:400) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291) [spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) [spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4813) [catalina.jar:8.0.35]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5272) [catalina.jar:8.0.35]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.35]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) [catalina.jar:8.0.35]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) [catalina.jar:8.0.35]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_151]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_151]

我在整个互联网上进行了搜索,但找不到任何解决方案。我最近升级到 spring 5.1.2。这是我用于 jpa 的依赖项

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>3.7.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>3.7.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>3.7.4</version>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>4.1.3</version>
        </dependency>

这是我正在使用的 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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/oxm 
        http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">

    <jee:jndi-lookup id="chatSysConfigId"
        jndi-name="java:comp/env/java/chatSysConfig" resource-ref="true" />

    <!-- bean declaration to read properties file -->
    <bean id="chatProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" ref="chatSysConfigId" />
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <ref bean="chatSysConfigId" />
            </list>
        </property>
    </bean>

    <context:annotation-config />
    <context:component-scan base-package="com.chat" />
    <mvc:annotation-driven />

    <jpa:repositories base-package="com.chat.dao.repository" />

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${chat.db.driverClass}" />
        <property name="jdbcUrl" value="${chat.db.jdbcUrl}" />
        <property name="username" value="${chat.db.user}" />
        <property name="password" value="${chat.db.password}" />
        <property name="maximumPoolSize" value="${chat.db.maxPoolSize}" />
        <property name="minimumIdle" value="${chat.db.minPoolSize}" />
        <property name="idleTimeout" value="${chat.db.idleConnectionTestPeriod}" />

        <property name="connectionTimeout" value="${chat.db.connectionTimeout}" />
        <property name="maxLifetime" value="${chat.db.maxLifetime}" />
        <property name="leakDetectionThreshold" value="${chat.db.leakDetectionThreshold}" />
        <property name="poolName" value="${chat.db.poolName}" />
        <property name="dataSourceProperties" ref="dataSourcePropertiesId" />
    </bean>

    <util:properties id="dataSourcePropertiesId">
        <prop key="cachePrepStmts">${chat.db.cachePrepStmts}</prop>
        <prop key="prepStmtCacheSize">${chat.db.prepStmtCacheSize}</prop>
        <prop key="prepStmtCacheSqlLimit">${chat.db.prepStmtCacheSqlLimit}</prop>
        <prop key="useServerPrepStmts">${chat.db.useServerPrepStmts}</prop>
    </util:properties>

    <!-- Transaction Manager -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="false" />
        <property name="database" value="${chat.db.database}" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaProperties">
          <props>
            <prop key="hibernate.id.new_generator_mappings">false</prop>
          </props>
        </property>
        <!-- spring based scanning for entity classes -->
        <property name="packagesToScan" value="com.chat.dao" />
    </bean>

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

</beans>

我在这里做错了什么。我错过了什么?在此之前,我遇到了一个 Maven 构建问题“无法访问 OrderSpecifier”,并在这里找到了我必须添加 jpa 依赖项的解决方案。哪个是

    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>4.1.3</version>
    </dependency>

这解决了构建问题,但现在我在部署时遇到了所有其他问题。

标签: spring-mvcspring-data-jpaquerydsl

解决方案


推荐阅读