4


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 ?

5
  • Have you defined your spring.datasource.driverclassname? Commented Nov 28, 2018 at 15:36
  • Are you using spring-data? Commented Nov 28, 2018 at 16:11
  • I have just tried adding the driverclasname but it didn't change. Also I'm using 'spring-boot-starter-data-jpa'. Does it make difference? Commented Nov 28, 2018 at 16:18
  • Are you certain that you provided the correct path to your sql files? Maybe you can try adding your sql files into the resources folder and remove the spring.datasource.data = classpath:/data.sql spring.datasource.schema = classpath:/schema.sql Commented Nov 28, 2018 at 16:26
  • @Carla - Were you able to solve this issue ? Commented Jul 10, 2019 at 7:04

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this process works for any DB ? I dont see its working with these configurations. Any pointers here ?
fyi, spring.datasource.initialization-mode=always seems now to have been replaced by spring.sql.init.mode=always (docs.spring.io/spring-boot/docs/current/reference/html/…)
1

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.

Comments

1

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

Comments

0

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.

1 Comment

Thanks, I have tried changing to 'none' but it still doesn't execute the SQL files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.