Spring Load H2 database memory does not load data from file on initialization

I have a problem loading data into a database in memory while initializing the application. I created the schema.sql and data.sql files containing the structure and source data of the table.

schema.sql:

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

and data.sql :

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

I use JpaRepositoryto work with the data layer:

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

And I also customize 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();

Custom object

@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 have to get two pre-populated users from my embedded H2 database. What is bad in a memory database? Thank.

+5
1

h2, INIT URL- ( ):

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

INIT. , INIT, , .

, application.properties:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=true
spring.datasource.initialize=true

. , , .

+5

Source: https://habr.com/ru/post/1667701/


All Articles