Spring and H2 Database - can't connect to h2 database

懵懂的女人 提交于 2020-05-16 02:32:08

问题


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

Now I add to my application.properties this line:

spring.jpa.hibernate.ddl-auto=create

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

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

What is wrong?

Thanks


回答1:


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



回答2:


I guess that by adding the explicit dependency to compile(group: 'javax.persistence', name: 'persistence-api', version: '1.0') in build.gradle you override the version of the transitive dependency and thus api and implementation are out of sync. Try to remove this dependency.



来源:https://stackoverflow.com/questions/61490113/spring-and-h2-database-cant-connect-to-h2-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!