简体   繁体   English

内存数据库 H2 中的 Spring Boot 在初始化时不会从文件加载数据

[英]Spring Boot in memory database H2 doesn't load data from file on initialization

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文件和包含表结构和初始数据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.我得到一个空列表,但我应该从我的内存 H2 数据库中获取两个预先填充的用户。 What's wrong with in memory database?内存数据库有什么问题? Thanks.谢谢。

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.此功能通过 INIT 属性启用。 Note that multiple commands may be passed to INIT, but the semicolon delimiter must be escaped, as in the example below.请注意,可以将多个命令传递给 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对于简单的情况,仅这些就足以自动构建表并在关闭和启动后重新加载

I solved a similar problem when I added the following lines to the application.properties:当我将以下几行添加到 application.properties 时,我解决了类似的问题:

spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.session.jdbc.initialize-schema=always
spring.sql.init.data-locations=classpath:schema.sql,classpath:data.sql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM