java - SpringBoot + Hibernate + EHCache 2.X AbstractMethodError异常

我无法使用Hibernate和ehcache 2.x配置SpringBoot项目。我正在尝试启用Hibernate的二级缓存。
我用的是:
SpringBoot 1.5.9 RELEASE
Hibernate 5.2.13.Final
EHCache 2.X(由Spring Boot Starter缓存依赖项导入)
此项目公开restapi,当我尝试使用终结点登录此api的应用程序时,将引发异常:

[INFO ] 2018-11-14 12:34:39.524 [main] TomcatEmbeddedServletContainer - Tomcat started on port(s): 8080 (http)
[INFO ] 2018-11-14 12:34:39.531 [main] Application - Started Application in 14.485 seconds (JVM running for 15.155)
nov 14, 2018 12:34:50 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause
java.lang.AbstractMethodError: org.hibernate.cache.ehcache.internal.nonstop.NonstopAwareEntityRegionAccessStrategy.get(Lorg/hibernate/engine/spi/SessionImplementor;Ljava/lang/Object;J)Ljava/lang/Object;
    at org.hibernate.engine.internal.CacheHelper.fromSharedCache(CacheHelper.java:32)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4410)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:510)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:165)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:840)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:822)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:827)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
    at com.sun.proxy.$Proxy151.merge(Unknown Source)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:511)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:520)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:505)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy184.save(Unknown Source)
    at net.gestion_casos.service.security.UserDetailsService.updateAgent(UserDetailsService.java:66)
    at net.gestion_casos.service.security.UserDetailsService.loadUserByUsername(UserDetailsService.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy185.loadUserByUsername(Unknown Source)
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

完整日志可以在以下链接中查看:https://ufile.io/9j69m
我已经为ehcache启用了调试日志记录,与之对应的日志行是用<<>>字符串预先挂起的,以便于查找它。
这篇文章暗示这可能是hibernate依赖之间的依赖问题。
我使用gradle来管理依赖关系,这是我的build.gradle,在这里可以看到我声明的所有依赖关系:
buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

def manifestPath = "src/main/resources/Manifest.txt"

Properties props = new Properties()
props.load(new FileInputStream(String.format("%s/%s", projectDir, manifestPath)))

group = 'net.integraciones.co.osi'
version = props.getProperty("Implementation-Version")
sourceCompatibility = 1.8

jar {
    baseName = 'service'
    version = version

    manifest {
        from(manifestPath)
    }
    ant.propertyfile(
            file: "env.properties") {
        entry(key: "CURRENT_VERSION", value: version)
    }
}

repositories {
    mavenCentral()
}

dependencies {

    compile('org.springframework.boot:spring-boot-starter-web:1.5.9.RELEASE') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    compile('org.springframework.boot:spring-boot-starter-security:1.5.9.RELEASE') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

    // log4j2
    compile 'org.apache.logging.log4j:log4j-api:2.5'
    compile 'org.apache.logging.log4j:log4j-core:2.5'
    // log4j2 bridge con slf4j
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.1'

    // Jackson
    compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.7.3'
    //compile 'com.fasterxml.jackson.core:jackson-databind:2.5.4'
    // support for Java 8 date/time types (specified in JSR-310 specification)
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.3'
    // https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-hibernate5
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.9.6'


    // springfox-swagger2
    compile 'io.springfox:springfox-swagger2:2.7.0'
    compile 'io.springfox:springfox-swagger-ui:2.7.0'

    // Database dependencies   
    compile (group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.10.RELEASE'){
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.2.13.Final'
    compile 'org.aspectj:aspectjweaver:1.8.9'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.45'

    compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.14.Final'
    compile('org.springframework.boot:spring-boot-starter-cache') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }   

    // Apache
    compile 'org.apache.commons:commons-lang3:3.0'
    compile('net.sf.dozer:dozer:5.4.0') {
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
    compile('com.google.guava:guava:21.0')

    compile group: 'org.springframework.ws', name: 'spring-ws-core', version: '3.0.0.RELEASE'

    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.16'

    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.11'   

    //test
    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-test-logging'
    }
    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
    testCompile group: 'org.apache.logging.log4j', name:'log4j-api', version: '2.5'
    testCompile group: 'org.apache.logging.log4j', name:'log4j-core', version: '2.5'
    testCompile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.10.0'
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.194'

}

“gradle dependencies”命令打印的依赖项树可以在这里查看:How to solve AbstractMethodError in Hibernate 5?
我还在springboot的application.yaml中配置了hibernate二级缓存。这是相关部分:
spring:
  datasource:
    name: mysql
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.41.211:3306/SpringBootProject?verifyServerCertificate=false&useSSL=false
    username: root
    password: root
  jpa:
    show-sql: true
    properties:
      javax:
        persistence:
          sharedCache:
            mode: ALL
      hibernate:
        cache:
          region:
            factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
          use_second_level_cache: true
        globally_quoted_identifiers: true
    hibernate:
      dialect: org.hibernate.dialect.MySQL5Dialect
      ddl-auto: validate
      naming:
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

server:
  port: 8080

我的ehcache.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ehcache>
<ehcache 
    name="MY_CACHE" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="true"
    monitoring="autodetect" 
    dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache 
        maxEntriesLocalHeap="10000" 
        eternal="false"
        timeToIdleSeconds="120" 
        timeToLiveSeconds="240" 
        maxEntriesLocalDisk="100000"
        diskExpiryThreadIntervalSeconds="240"
        memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name=a.project.package.Catalog" maxEntriesLocalHeap="1000" />

</ehcache>

最佳答案

我能解决这个问题。
问题是我在build.gradle中声明了一个错误的hibernate ehcache依赖版本。
在ehcache文档中,hibernate要能够将其用作二级缓存,hibernate ehcache依赖项必须与springboot使用的hibernate核心依赖项版本匹配。
当我在build.gradle中声明ehcache的依赖性时,我就知道这一点,但是我没有使用hibernate核心版本,而是使用hibernate-java8版本。
通过更正hibernate ehcache版本,问题得以解决。

本文翻译自 https://stackoverflow.com/questions/53304421/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 java spring hibernate spring-boot ehcache-2


相关文章:

java - Glassfish不会加载jdbc

java - 没有在Spring Data Rest中获取ManyToOne渴望的关联

java - 如何设置在Java JPA / Hibernate中未自动生成的@Id主键?

java - 如何在休眠状态下处理多个会话事务的提交和回滚?

java - 杰克逊反序列化内部集合

java - 准备语句如何比语句更好地防止SQL注入?

java - 创建在类路径资源META-INF / cxf / cxf.xml中定义的名称为“ cxf”的bean时出错

java - 我必须在Spring引导中使用POST方法(Restfull)在where子句中从具有多个参数的MySQL Db表中搜索?

java - Hibernate期待“全部”,发现'('-这是什么意思?

java - 如何批量检查空字符串中的许多变量?