Spring和H2数据库-无法连接到H2数据库 [英] Spring and H2 Database - can't connect to h2 database

查看:1686
本文介绍了Spring和H2数据库-无法连接到H2数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to connect to my h2 database. My application.properties is empty. When I try to connect I get the following error message:

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中:

Now I add to my application.properties this line:

spring.jpa.hibernate.ddl-auto=create

现在,当我尝试重新运行Spring应用程序时,我得到以下错误消息:

Now I get this error message when I try to rerun my Spring application:

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(依赖项)如下:

My build.gradle (dependencies) looks like this:

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'
}

怎么了?

谢谢

推荐答案

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

Add h2 configuration to your 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

这篇关于Spring和H2数据库-无法连接到H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆