简体   繁体   中英

Unable to execute data.sql and schema.sql at Spring Boot Startup


I'm trying to execute some DB initialization for a Spring Boot application against a MySQL Database that is running in a container. During the authentication process, I receive an error "Table not found". I've checked the DB and no tables have been created indeed. Is there something missing in DB properties?

spring.datasource.url = jdbc:mysql://172.17.0.2:3306/schema
spring.datasource.username = user
spring.datasource.password = password
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.data.rest.basePath=/
spring.datasource.data = classpath:/data.sql
spring.datasource.schema = classpath:/schema.sql

All in all, the JDBC Settings work fine provided that I create the DDL from the mysql command line. So it's just not executing the data.sql and schema.sql at startup. Do I need some extra properties for mysql?

You can update the application.properties with following properties.

application.properties:

spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
spring.datasource.initialization-mode=always

By default value of spring.jpa.generate-ddl is false. If you set spring.jpa.generate-ddl=true or spring.jpa.hibernate.ddl-auto to any of value validate , update , create , create-drop . Spring boot generates schema scripts and creates the tables based on the entities available in your application.

Now you should create the schema.sql and data.sql files under resources folder and set the property spring.datasource.initialization-mode=always in application.properties.

You can also run the scripts based on the platform. Let's suppose you want to run the scripts for hsqldb database, then set spring.datasource.platform=hsqldb in application.properties file and created scripts file schema-hsqldb.sql and data-hsqldb.sql under resources folder. You can find more details about how to load schema.sql and data.sql on startup with spring boot .

As you have

spring.jpa.hibernate.ddl-auto = create-drop

Spring doesn't run the schema.sql and data.sql.

Try it with

spring.jpa.hibernate.ddl-auto = none

Check the docs

In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.

For having a schema and a table created from your entity class, please use the following setup:

application.properties:

# Mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myschema?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true
spring.datasource.username=user
spring.datasource.password=pass

# Hibernate
spring.datasource.platform=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.data.jpa.repositories.enabled=true

The important setup here is the spring.datasource.url with the createDatabaseIfNotExist=true parameter and the spring.jpa.hibernate.ddl-auto=update

Your entity class should look something like this:

@Entity
public class MyEntity implements Serializable {

    @Id
    @Column(name = "id", columnDefinition = "BIGINT(20)")
    private Long id;
    @Column(name = "int_column", columnDefinition = "INT(4)")
    private Integer intColumn;
    @Column(name = "string_column", columnDefinition = "VARCHAR(15)")
    private String stringColumn;
    // more fields here with getters, setters, etc...
}
  • You can define the table columns with the columnDefinition setting of the @Column annotation. Here you can also use the unique=true setting.

  • Your table will be named after your class but you can also change that with @Table("my_custom_name") . You can also set unique constraints here.

  • For auto-incrementing IDs, use @GeneratedValue(strategy = GenerationType.IDENTITY) annotation on your id field.

Hope this helps

Everything works as expected until a spring-boot update the pom file from

<version>1.5.8.RELEASE</version>

to

<version>2.3.3.RELEASE</version>

A switch back and update all test are passed without any exceptions. In Version 2.3.3 a test fault caused by missing data in database. I've spend hours to solve the ddl initialization for Spring Version 2.3.3.RELEASE without find regular solution.

For spring boot version 2.7, ensure that you have configured spring.sql.init.mode property in the application.properties file

Example:

spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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