java 内存数据库 H2 中的 Spring Boot 不会在初始化时从文件加载数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41841072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot in memory database H2 doesn't load data from file on initialization
提问by tyomka
I have an issue with loading data into an in-memory database on application initialization. I have created schema.sqland data.sqlfiles containing table structure and initial data.
我在应用程序初始化时将数据加载到内存数据库时遇到问题。我已经创建schema.sql文件和data.sql含有表结构和初始数据文件。
schema.sql :
架构.sql:
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(64) NOT NULL,
password VARCHAR(64)
);
and data.sql:
和data.sql:
INSERT INTO users (id, username, password) VALUES
(1, 'usr1', 'bigSecret'),
(2, 'usr2', 'topSecret');
I am using JpaRepository
for working with data layer:
我JpaRepository
用于处理数据层:
public interface UserRepository extends JpaRepository<User, Long> {
}
And I also configure application.properties
而且我还配置了application.properties
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
But when I call
但是当我打电话
List<User> users = userRepository.findAll();
User entity
用户实体
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue
private Long id;
private String username;
private String password;
public User() { }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I get an empty list, but I should get two pre-populated users from my in-memory H2 database. What's wrong with in memory database? Thanks.
我得到一个空列表,但我应该从我的内存 H2 数据库中获取两个预先填充的用户。内存数据库有什么问题?谢谢。
采纳答案by Maciej Kowalski
You can always try to run those scripts per specification of h2, where you should add an INIT script in your connection url (being one of the options):
您始终可以尝试按照 h2 的规范运行这些脚本,您应该在连接 url 中添加一个 INIT 脚本(作为选项之一):
jdbc:h2:mem:test;INIT=RUNSCRIPT FROM '~/schema.sql'\;RUNSCRIPT FROM '~/data.sql'"
This functionality is enabled via the INIT property. Note that multiple commands may be passed to INIT, but the semicolon delimiter must be escaped, as in the example below.
此功能通过 INIT 属性启用。请注意,可以将多个命令传递给 INIT,但必须对分号分隔符进行转义,如下例所示。
Update
更新
Be aware that having these options in your application.properties
:
请注意,在您的application.properties
:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=true
spring.datasource.initialize=true
may cause some clashing during startup. So you should always aim for one or the other, but never both at the same time. For simple cases just these alone are sufficient to auto build tables and reload after shutdown & startup
可能会在启动过程中引起一些冲突。因此,您应该始终瞄准其中一个,但切勿同时瞄准两者。对于简单的情况,仅这些就足以自动构建表并在关闭和启动后重新加载