内存数据库H2中的Spring Boot不会在初始化时从文件加载数据 [英] Spring Boot in memory database H2 doesn't load data from file on initialization

查看:553
本文介绍了内存数据库H2中的Spring Boot不会在初始化时从文件加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序初始化时将数据加载到内存数据库中时遇到问题.我创建了包含表结构和初始数据的 schema.sql data.sql 文件.

I have an issue with loading data into an in-memory database on application initialization. I have created schema.sql and data.sql files containing table structure and initial data.

schema.sql:

CREATE TABLE users (
  id          INT PRIMARY KEY,
  username    VARCHAR(64) NOT NULL,
  password    VARCHAR(64) 
);

data.sql :

INSERT INTO users (id, username, password) VALUES
  (1, 'usr1', 'bigSecret'),
  (2, 'usr2', 'topSecret');

我正在使用JpaRepository处理数据层:

I am using JpaRepository for working with data layer:

public interface UserRepository extends JpaRepository<User, Long> {
}

我还配置了 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=

但是当我打电话

List<User> users = userRepository.findAll();

用户实体

@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;
  }
}

我得到一个空列表,但我应该从内存中的H2数据库中获得两个预先填充的用户.内存数据库有什么问题? 谢谢.

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的规范运行这些脚本,在此应在连接URL中添加INIT脚本(作为选项之一):

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):

jdbc:h2:mem:test;INIT=RUNSCRIPT FROM '~/schema.sql'\;RUNSCRIPT FROM '~/data.sql'"

此功能通过INIT属性启用.注意 可以将多个命令传递给INIT,但是分号分隔符 必须转义,如以下示例所示.

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.

更新

请注意,在您的application.properties中有以下选项:

Be aware that having these options in your 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

这篇关于内存数据库H2中的Spring Boot不会在初始化时从文件加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆