0

I have a simple spring boot application with H2 in-memory database and trying to initalize data.

This is my application.config:

#Database connection
spring.datasource.url=jdbc:h2:mem:practice_db
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.driverClassName=org.h2.Driver

#spring.datasource.initialize=true
#spring.datasource.schema=schema.sql
#spring.datasource.data=schema.sql


#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none

schema.sql

CREATE TABLE IF NOT EXISTS `Person` (
    `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
    `first_name` VARCHAR(50) NOT NULL,
    `age`        INTEGER  NOT NULL
);

INSERT INTO `Person` (`first_name`, `age`) VALUES ('Peter', 20);

and data.sql

INSERT INTO `Person` (`first_name`, `age`) VALUES ('John', 20);

This is my entity

/**
 * Dummy data model.
 * Just for test
 */
@Entity
@Table(name = "Person")
public class Person {

    @Id
    @GeneratedValue
    @Column(name = "Id")
    private Long id;

    /**
     * For hibernate
     */
    @Version
    private Integer version;

    /**
     * Person name
     */
    @Basic(optional = false)
    @Column(name = "first_name")
    private String name;

    /**
     * Age. Primitive type can't be nullable
     */
    @Basic(optional = false)
    @Column(name = "age")
    private int age;

Table 'Person' is created succesfully. But it has no data, although the script was executed:

09:06:29.711 5823 [main] INFO  o.s.j.datasource.init.ScriptUtils - Executing SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/schema.sql]
09:06:29.732 5844 [main] INFO  o.s.j.datasource.init.ScriptUtils - Executed SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/schema.sql] in 21 ms.
09:06:29.742 5854 [main] INFO  o.s.j.datasource.init.ScriptUtils - Executing SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/data.sql]
09:06:29.748 5860 [main] INFO  o.s.j.datasource.init.ScriptUtils - Executed SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/data.sql] in 1 ms.
09:06:29.915 6027 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default'
09:06:29.949 6061 [main] INFO  o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [

When application is running, writing and reading data from this table works fine.

How can I initalize data on application startup?

hiropon
  • 1,675
  • 2
  • 18
  • 41
Kirill
  • 1,540
  • 4
  • 18
  • 41
  • Possible duplicate of https://stackoverflow.com/questions/38040572/spring-boot-loading-initial-data – Afridi May 31 '17 at 06:38
  • @Afridi I don't have PersonRepository. I save data in application with EntityManager.persist. g00glen00b sais that he need data.sql file with INSERT statement. I have such file, but it doesn't work for some reason – Kirill May 31 '17 at 06:44

1 Answers1

2

In accordance with Spring documentation, Hibernate has the following defaults:

spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the hibernate.hbm2ddl.auto property. Default to create-drop when using an embedded database, "none" otherwise.

Thus, for embedded database your SQL scripts are launched and then Hibernate drops the database and recreates the schema in accordance with your model. Therefore, you do not see the insert any more.

As a solution you can just set spring.jpa.hibernate.ddl-auto=none in application.properties.

Michael Potter
  • 530
  • 4
  • 11
  • Both DML and DDL operations are performed correctly (using Spring JDBC), but after that Hibernate comes, drops the schema and launches its own DDL operation. Therefore, the original DML records are lost. – Michael Potter May 31 '17 at 06:58