我有一个H2数据库的URL“jdbc: H2:test”。我使用create table PERSON (ID INT主键,名字VARCHAR(64),姓VARCHAR(64));;然后使用select * from PERSON从这个(空)表中选择所有内容。到目前为止,一切顺利。

然而,如果我将URL更改为“jdbc:h2:mem:test”,唯一的区别是数据库现在只在内存中,这给了我一个org.h2.jdbc。JdbcSQLException:表“PERSON”未找到;SQL语句:SELECT * FROM PERSON[42102-154]。我可能错过了一些简单的东西,但任何帮助将不胜感激。


当前回答

当打开h2-console时,JDBC URL必须与属性中指定的URL匹配:

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

spring.h2.console.enabled=true

这似乎是显而易见的,但我花了几个小时来解决这个问题。

其他回答

也有类似的问题 解决方案是向application.properties添加以下内容

spring.jpa.defer-datasource-initialization=true

在我的情况下,缺少表错误发生在jpa测试期间,表是由方案创建的。SQL文件,问题在放置@org.springframework.transaction.annotation后被修复。测试中的事务性

我已经尝试了上面的解决方案,但在我的情况下,在控制台中添加属性DB_CLOSE_ON_EXIT=FALSE,它修复了这个问题。

 spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=false;DB_CLOSE_ON_EXIT=FALSE

一个原因可能是jpa试图在创建表结构之前插入数据,为了解决这个问题,在应用程序中插入这一行。属性:

spring.jpa.defer-datasource-initialization=true
   Use the same in applications.properties file
   
   spring.jpa.show-sql=true
   spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=false
   DB_CLOSE_ON_EXIT=FALSE
   spring.data.jpa.repositories.bootstrap-mode=default
   spring.h2.console.enabled=true spring.jpa.generate-ddl=true
   spring.jpa.hibernate.ddl-auto=create
   spring.datasource.driverClassName=org.h2.Driver
   spring.jpa.defer-datasource-initialization=true