为什么每次我启动我的Spring Boot应用程序时,我的h2数据库都是空的?

huangapple go评论77阅读模式
英文:

Why is my h2 database empty every time my Spring Boot application starts?

问题

我在一个使用 Maven 的项目中使用 Spring Boot,并依赖于 spring-boot-starter-data-jpa。

在我的 application.yml 文件中,我有:

spring.datasource.url: jdbc:h2:./data;DB_CLOSE_ON_EXIT=FALSE

我还有一个名为 DataPopulation 的类,其中包含:

if (userRepository.findAll().iterator().hasNext()) {
  // 数据库中已经有用户...
}

其中,userRepository 是一个 CrudRepository 实例。

每次启动时,userRepository 都不会返回任何用户,无论上次应用程序运行时添加了多少用户。

我可以看到已经创建了 data.mv.db 和 data.trace.db 文件,并且它们不是空的。

为什么我的数据库在启动时总是为空?我漏掉了什么?

英文:

I'm using spring boot in a maven project with the spring-boot-starter-data-jpa dependency.

In my application.yml file I have:

spring.datasource.url: jdbc:h2:./data;DB_CLOSE_ON_EXIT=FALSE

I also have a DataPopulation class with :

if (userRepository.findAll().iterator().hasNext()) {
  // Database already has users..
}

Where userRepository is a CrudRepository instance.

On every start up the userRepository returns no users, regardless of how many were added the last time the application was running.

I can see that the data.mv.db and data.trace.db files are created and not empty.

Why is my database always empty on start up? What am I missing?

答案1

得分: 4

./data路径让我觉得每次构建应用程序时数据库都会被删除,因为.表示当前目录。所以你可以使用一个相对于用户主文件夹的数据库文件,表示为~~/data。你也可以在JDBC URL中使用:file

spring.datasource.url: jdbc:h2:file:~/data;DB_CLOSE_ON_EXIT=FALSE

如果需要的话,可以查阅H2数据库的特性文档

英文:

The ./data path makes me think the database is getting deleted each time you build the application, as . means the current directory. So you could use a database file relative your your user home folder, indicated by ~: ~/data. You also may want to use :file in the JDBC URL:

spring.datasource.url: jdbc:h2:file:~/data;DB_CLOSE_ON_EXIT=FALSE

In case you need, check the H2 features documentation.

答案2

得分: 2

以下是翻译好的部分:

我的jdbc字符串没有任何问题。Spring Boot将'create-drop'用作默认的Hibernate ddl-auto值。

我通过添加以下内容解决了这个问题:

spring.jpa.hibernate.ddl-auto=update

到我的属性中。

英文:

There was nothing wrong my jdbc string. Spring boot uses 'create-drop' as the default hibernate ddl-auto value.

I resolved the issue by adding:

spring.jpa.hibernate.ddl-auto=update

to my properties.

答案3

得分: 2

根据 https://www.baeldung.com/spring-boot-h2-database,按照默认设置,data.sql 会在 Hibernate 初始化之前被执行。因此,我们需要延迟数据源的初始化,如下所示。这确保数据在架构生成之后被填充。
spring.jpa.defer-datasource-initialization=true

以下是我的工作 application.properties 文件内容:

spring.h2.console.enabled=true
spring.sql.init.platform=h2
spring.datasource.url=jdbc:h2:mem:lee
spring.jpa.defer-datasource-initialization=true

英文:

As per https://www.baeldung.com/spring-boot-h2-database, by default data.sql gets executeed ahead of Hibernate initialisation. Therefore, we have to defer datasource initialisation as given below. This ensures that the data is populated after the schema is generated.
spring.jpa.defer-datasource-initialization=true

Following is my working application.properties

spring.h2.console.enabled=true
spring.sql.init.platform=h2
spring.datasource.url=jdbc:h2:mem:lee
spring.jpa.defer-datasource-initialization=true

答案4

得分: 1

你需要以以下形式声明URL:

jdbc:h2:file:<dbpath>

示例:

jdbc:h2:file:/opt/db/mydb

以持久化你的数据。

英文:

You need to declare the url in this form:

jdbc:h2:file:<dbpath>

example

jdbc:h2:file:/opt/db/mydb

to persist your data.

huangapple
  • 本文由 发表于 2020年7月27日 17:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63112686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定