【问题标题】:Spring and H2 Database - can't connect to h2 databaseSpring 和 H2 数据库 - 无法连接到 h2 数据库
【发布时间】:2020-08-12 20:26:24
【问题描述】:

我尝试连接到我的 h2 数据库。我的 application.properties 是空的。当我尝试连接时,我收到以下错误消息:

Database "mem:/~/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149

现在我将这一行添加到我的 application.properties 中:

spring.jpa.hibernate.ddl-auto=create

现在当我尝试重新运行我的 Spring 应用程序时收到此错误消息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-28 23:08:20.102 ERROR 15728 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor.getValidationMode(PersistenceUnitInfoDescriptor.java:88)

The following method did not exist:

    javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

The method's class, javax.persistence.spi.PersistenceUnitInfo, is available from the following locations:

    jar:file:/C:/Users/x/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar!/javax/persistence/spi/PersistenceUnitInfo.class
    jar:file:/C:/Users/x/.gradle/caches/modules-2/files-2.1/jakarta.persistence/jakarta.persistence-api/2.2.3/8f6ea5daedc614f07a3654a455660145286f024e/jakarta.persistence-api-2.2.3.jar!/javax/persistence/spi/PersistenceUnitInfo.class

It was loaded from the following location:

    file:/C:/Users/x/.gradle/caches/modules-2/files-2.1/javax.persistence/persistence-api/1.0/5725f57873e05e068803e2bf9d5a8ea3740ffec5/persistence-api-1.0.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.persistence.spi.PersistenceUnitInfo

我的 build.gradle(依赖项)如下所示:

dependencies {
    compile(group: 'javax.persistence', name: 'persistence-api', version: '1.0')
    compile(group: 'org.apache.directory.api', name: 'api-all', version: '2.0.1')
    compile 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

怎么了?

谢谢

【问题讨论】:

    标签: java spring-boot jpa h2


    【解决方案1】:

    将 h2 配置添加到您的 application.properties

    spring.datasource.url=jdbc:h2:mem:DBNAME
    spring.datasource.username=root
    spring.datasource.password=SA
    spring.datasource.driverClassName=org.h2.Driver
    spring.h2.console.enabled=true
    spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
    spring.datasource.initialize=true
    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    

    【讨论】:

      【解决方案2】:

      我猜想通过在build.gradle 中向compile(group: 'javax.persistence', name: 'persistence-api', version: '1.0') 添加显式依赖项,您将覆盖传递依赖项的版本,因此api 和实现不同步。尝试删除此依赖项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 2020-09-03
        • 1970-01-01
        相关资源
        最近更新 更多