Spring Boot running data.sql before creating entities in presence of schema.sql
See original GitHub issueI’m trying to insert some rows in my entities for testing on developer machine using H2 Database. I’m using data.sql for this.
It works fine, entities are created and then data.sql is run to insert data in the tables produced by the entities.
However I need to create some other tables for which there are no entity classes, so I’m using schema.sql for those. Here’s the issue, as soon as I add schema.sql to the project, Spring Boot runs data.sql before creating entities, which ends in Table not found exception.
It works fine if I replace data.sql with import.sql, it’s run after the entities are created.
However h2 configuration is supposed to run only when testing, for that I have a maven profile which activates a spring.datasource.platform = h2, to load schema-h2.sql and data-h2.sql. import.sql doesn’t work with the platform.
So the issue is that data.sql is being run before entities are created only when schema.sql is present.
Here’s the Github repository for reproducing the issue
Without any platform
https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue.git
With h2 platform (It’s another branch platform-h2 in the above repository)
https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue/tree/platform-h2
Stackoverflow
Thanks
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:11 (5 by maintainers)

Top Related StackOverflow Question
For now I have renamed the
data-h2.sqltosample_data_h2.sqland I’m loading it using ScriptUtils in @PostConstruct method of a bean with conditional @ProfileCurrently the
schema.sqlfile is taken to bean that the entire database definition is being created by that file. It would be nice if we could changeDataSourceInitializerso that theschema.sqlfile doesn’t immediately trigger initialization. The ideal order is:With hibernate:
Without hibernate:
This are of the code is unfortunately quite difficult to change without causing side effects. I don’t think we’ll be able to do anything in the 1.5.x line.
I’d suggest you either try manually adding the create table commands for the entities in
schema.sqlor trying flyway for data population.