Hibernate v6,缓存|XmlExceptionException:分析XML配置时出错

gcuhipw9  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(195)

我有一个正在工作的hibernatev 6项目,想添加缓存,但我无法让它工作。
我得到这个错误(减少)

  • 第一个月
  • Caused by: javax.cache.CacheException: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/C:/Users/admin/git/HibernateCaching/build/resources/main/ehcache.xml
  • Caused by: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/C:/Users/admin/git/HibernateCaching/build/resources/main/ehcache.xml
  • Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath. with linked exception: [java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
  • Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory

->结尾处的完整错误
我已经尝试了几种方法来设置缓存,但init总是失败。

最小示例:

项目结构:

my-project
├── src/
│   ├── main/
│   │   └── java/
│   │       └── dev.carryout.hc/
│   │           ├── db/
│   │           │   └── DBExample.java
│   │           └── main/
│   │               └── Main.java
│   └── resources/
│       └── ehcache.xml
└── build.gradle

字符串
build.gradle

apply plugin: 'java'

task runMain(type: JavaExec) {
    group 'run'
    main = 'dev.carryout.hc.main.Main'
    classpath = sourceSets.main.runtimeClasspath
    args('--add-modules', 'java.xml.bind')
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    mavenCentral()
    dependencies {
        implementation 'org.hibernate:hibernate-core:6.2.1.Final'
        implementation 'org.mariadb.jdbc:mariadb-java-client:3.1.3'
        compileOnly 'org.projectlombok:lombok:1.18.26'
        implementation 'org.hibernate.orm:hibernate-jcache:6.2.1.Final'
        implementation 'org.ehcache:ehcache:3.10.8'
        implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
        implementation 'javax.activation:activation:1.1.1'
        implementation 'com.sun.xml.bind:jaxb-impl:4.0.2'
        implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.0'
        implementation 'javax.xml.bind:jaxb-api:2.3.1'
    }
}


Main.java

package dev.carryout.hc.main;

import dev.carryout.hc.db.DBExample;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MariaDBDialect;
import org.mariadb.jdbc.Driver;

public class Main {

    private static SessionFactory factory;

    public static void main(String[] args) {
        System.out.println("--- start ---");
        initDB();
        // use database
        System.out.println("--- end ---");
    }

    private static void initDB() {
        Main.factory = getConfig().buildSessionFactory();
    }

    private static Configuration getConfig() {

        Configuration config = new Configuration();

        config.setProperty("hibernate.hbm2ddl.auto", "update");
        config.setProperty("hibernate.dialect", MariaDBDialect.class.getName());
        config.setProperty("hibernate.connection.driver_class", Driver.class.getName());
        config.setProperty("hibernate.connection.url", "jdbc:mariadb://%s:%s/%s".formatted(
                "127.0.0.1",
                "3430",
                "hibernate-test"
        ));
        config.setProperty("hibernate.connection.username", "hibernate-test");
        config.setProperty("hibernate.connection.password", "my-password");

        // caching
        config.setProperty("hibernate.cache.region.factory_class", "jcache");
        config.setProperty("hibernate.javax.cache.provider", "org.ehcache.jsr107.EhcacheCachingProvider");
        config.setProperty("hibernate.javax.cache.uri", "ehcache.xml");
        config.setProperty("hibernate.cache.use_second_level_cache", "true");

        config.addAnnotatedClass(DBExample.class);

        return config;

    }

}


DBExample.java

package dev.carryout.hc.db;

import jakarta.persistence.Cacheable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import java.util.UUID;

@Entity
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class DBExample {

    @Id
    @Getter
    private UUID uuid;

    @Getter
    @Setter
    @Column
    private String content;

}


ehcache.xml

<ehcache>
    <defaultCache
        maxElementsInMemory="1000"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
    />
</ehcache>

调试信息

runtimeClasspath部分来自gradle dependencies

runtimeClasspath - Runtime classpath of source set 'main'.
+--- org.hibernate:hibernate-core:6.2.1.Final
|    \--- org.hibernate.orm:hibernate-core:6.2.1.Final
|         +--- jakarta.persistence:jakarta.persistence-api:3.1.0
|         +--- jakarta.transaction:jakarta.transaction-api:2.0.1
|         +--- org.jboss.logging:jboss-logging:3.5.0.Final
|         +--- org.hibernate.common:hibernate-commons-annotations:6.0.6.Final
|         +--- io.smallrye:jandex:3.0.5
|         +--- com.fasterxml:classmate:1.5.1
|         +--- net.bytebuddy:byte-buddy:1.12.18
|         +--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.0
|         |    \--- jakarta.activation:jakarta.activation-api:2.1.0 -> 2.1.1
|         +--- org.glassfish.jaxb:jaxb-runtime:4.0.2
|         |    \--- org.glassfish.jaxb:jaxb-core:4.0.2
|         |         +--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.0 (*)
|         |         +--- jakarta.activation:jakarta.activation-api:2.1.1
|         |         +--- org.eclipse.angus:angus-activation:2.0.0
|         |         |    \--- jakarta.activation:jakarta.activation-api:2.1.1
|         |         +--- org.glassfish.jaxb:txw2:4.0.2
|         |         \--- com.sun.istack:istack-commons-runtime:4.1.1
|         +--- jakarta.inject:jakarta.inject-api:2.0.1
|         \--- org.antlr:antlr4-runtime:4.10.1
+--- org.mariadb.jdbc:mariadb-java-client:3.1.3
|    \--- com.github.waffle:waffle-jna:3.2.0
|         +--- net.java.dev.jna:jna:5.12.1
|         +--- net.java.dev.jna:jna-platform:5.12.1
|         |    \--- net.java.dev.jna:jna:5.12.1
|         +--- org.slf4j:jcl-over-slf4j:1.7.36
|         |    \--- org.slf4j:slf4j-api:1.7.36
|         +--- org.slf4j:slf4j-api:1.7.36
|         +--- com.github.ben-manes.caffeine:caffeine:2.9.3
|         |    +--- org.checkerframework:checker-qual:3.19.0 -> 3.23.0
|         |    \--- com.google.errorprone:error_prone_annotations:2.10.0
|         \--- org.checkerframework:checker-qual:3.23.0
+--- org.hibernate.orm:hibernate-jcache:6.2.1.Final
|    +--- org.hibernate.orm:hibernate-core:6.2.1.Final (*)
|    +--- javax.cache:cache-api:1.0.0 -> 1.1.0
|    \--- org.jboss.logging:jboss-logging:3.5.0.Final
+--- org.ehcache:ehcache:3.10.8
|    +--- javax.cache:cache-api:1.1.0
|    +--- org.slf4j:slf4j-api:1.7.36
|    \--- org.glassfish.jaxb:jaxb-runtime:[2.2,3) -> 4.0.2 (*)
+--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.0 (*)
+--- javax.activation:activation:1.1.1
+--- com.sun.xml.bind:jaxb-impl:4.0.2
|    \--- com.sun.xml.bind:jaxb-core:4.0.2
|         +--- jakarta.xml.bind:jakarta.xml.bind-api:4.0.0 (*)
|         \--- org.eclipse.angus:angus-activation:2.0.0 (*)
+--- org.glassfish.jaxb:jaxb-runtime:4.0.0 -> 4.0.2 (*)
\--- javax.xml.bind:jaxb-api:2.3.1
     \--- javax.activation:javax.activation-api:1.2.0


日志错误:

> Task :runMain FAILED
--- start ---
Apr. 25, 2023 12:13:32 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.1.Final
Apr. 25, 2023 12:13:32 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: org.mariadb.jdbc.Driver
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:mariadb://127.0.0.1:3430/hibernate-test]
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=hibernate-test}
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Apr. 25, 2023 12:13:32 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MariaDBDialect, version: 10.8
Apr. 25, 2023 12:13:33 PM org.hibernate.bytecode.internal.BytecodeProviderInitiator buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : bytebuddy
Apr. 25, 2023 12:13:33 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mariadb://127.0.0.1:3430/hibernate-test]
Apr. 25, 2023 12:13:33 PM org.hibernate.cache.spi.AbstractRegionFactory stop
WARN: HHH90001002: Attempt to restop an already stopped JCacheRegionFactory.
Exception in thread "main" java.lang.IllegalStateException: Cache provider not started
        at org.hibernate.cache.spi.AbstractRegionFactory.verifyStarted(AbstractRegionFactory.java:63)
        at org.hibernate.cache.jcache.internal.JCacheRegionFactory.getOrCreateCache(JCacheRegionFactory.java:90)
        at org.hibernate.cache.jcache.internal.JCacheRegionFactory.createDomainDataStorageAccess(JCacheRegionFactory.java:85)
        at org.hibernate.cache.jcache.internal.JCacheRegionFactory.buildDomainDataRegion(JCacheRegionFactory.java:74)
        at org.hibernate.cache.internal.EnabledCaching.prime(EnabledCaching.java:114)
        at org.hibernate.internal.SessionFactoryImpl.primeSecondLevelCacheRegions(SessionFactoryImpl.java:574)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:252)
        at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:431)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:894)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:913)
        at dev.carryout.hc.main.Main.initDB(Main.java:21)
        at dev.carryout.hc.main.Main.main(Main.java:15)
Caused by: javax.cache.CacheException: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/C:/Users/admin/git/HibernateCaching/build/resources/main/ehcache.xml
        at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:331)
        at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:134)
        at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:85)
        at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:194)
        at org.hibernate.cache.jcache.internal.JCacheRegionFactory.resolveCacheManager(JCacheRegionFactory.java:208)
        at org.hibernate.cache.jcache.internal.JCacheRegionFactory.prepareForUse(JCacheRegionFactory.java:189)
        at org.hibernate.cache.spi.AbstractRegionFactory.start(AbstractRegionFactory.java:89)
        at org.hibernate.cache.internal.EnabledCaching.<init>(EnabledCaching.java:83)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:30)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:23)
        at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:63)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:239)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:216)
        at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:100)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:229)
        ... 5 more
Caused by: org.ehcache.xml.exceptions.XmlConfigurationException: Error parsing XML configuration at file:/C:/Users/admin/git/HibernateCaching/build/resources/main/ehcache.xml
        at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:126)
        at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:92)
        at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:328)
        ... 20 more
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
        at org.ehcache.xml.ConfigurationParser.<init>(ConfigurationParser.java:116)
        at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:116)
        ... 22 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
        at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
        ... 27 more

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':runMain'.
> Process 'command 'C:\Program Files\Microsoft\jdk-17.0.5.8-hotspot\bin\java.exe'' finished with non-zero exit value 1

  • 我添加了缓存依赖项。
  • 我添加了这些缓存配置属性:
config.setProperty("hibernate.cache.region.factory_class", "jcache");
config.setProperty("hibernate.javax.cache.provider", "org.ehcache.jsr107.EhcacheCachingProvider");
config.setProperty("hibernate.javax.cache.uri", "ehcache.xml");
config.setProperty("hibernate.cache.use_second_level_cache", "true");

  • 添加了ehcache.xml
  • 添加缓存注解到我的实体@Cacheable@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

我还将args('--add-modules', 'java.xml.bind')添加到gradle任务中
我总是得到一个错误。
我不确定是我的依赖项还是我的配置中的某些东西出错了,因为我应该有所有必需的依赖项。

23c0lvtd

23c0lvtd1#

我自己也为这个死了好几天。
先试试这个...
实现'org.ehcache:ehcache:3.10.8:jakcache'
我一直在使用spring-boot-starter-cache,但也遇到了同样的XML解析错误。我自己一直在摆弄各种XML解析器,直到我将其更改为上面的版本,才有所帮助.

相关问题