基于文件的h2持久但未在Spring Boot中加载 [英] File based h2 persisted but not loaded in Spring Boot
问题描述
我根据Spring Boot制作了一个小应用程序:
I made a small application based on Spring Boot:
- spring-boot-starter-web
- spring-boot-starter-data-jpa
该应用程序只有一个域类 Post。的java
。
因此有一个RestController和一个DAO。
数据应该保存在基于文件的hsql db中。
The application has simply one domain class Post.java
.
Accordingly there is a RestController and a DAO.
The data is supposed to be persisted in a file based hsql db.
当应用程序运行时,一切似乎都很好。数据存储。
h2文件已创建并包含插入语句。
When the application is running everything seems fine. Data is stored. The h2 file is created and contains insert statements.
然而,当我终止应用程序并再次启动它时。没有加载数据。 (好像创建了一个全新的db文件,它覆盖了旧文件)。
However, when I kill the application and start it a second time. No data is loaded. (As if a brand new db file was created, which overwrote the old one).
application.properties
spring.datasource.url = jdbc:h2:file:~/testdb
spring.datasource.username = sa
spring.datasource.password = sa
spring.datasource.driverClassName = org.h2.Driver
pom.xml
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- H2 DB -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.179</version>
</dependency>
PostDAO.java
public interface PostDAO extends JpaRepository<Post, Integer>{
public Post findByMessage(String message);
}
Post.java
@Entity
public class Post {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String message;
public Post(){
}
public Post(String message) {
super();
this.message = message;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
推荐答案
如果您使用嵌入式数据库, spring.jpa.hibernate.ddl-auto
的默认值为 create-drop
。你可能希望它是空的,或只是验证
(无
可能也可以,但我认为hibernate不推荐这样做)。
The default for spring.jpa.hibernate.ddl-auto
is create-drop
if you use an embedded database. You probably want it to be empty, or just validate
(none
might work as well but I think that's deprecated by hibernate).
这篇关于基于文件的h2持久但未在Spring Boot中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!