question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Boot running data.sql before creating entities in presence of schema.sql

See original GitHub issue

I’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

http://stackoverflow.com/questions/43692115/spring-boot-running-data-sql-before-creating-entities-in-presence-of-schema-sql

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
ConsciousObservercommented, May 3, 2017

For now I have renamed the data-h2.sql to sample_data_h2.sql and I’m loading it using ScriptUtils in @PostConstruct method of a bean with conditional @Profile

@Configuration
@Profile("h2_in_memory")
public class InMemoryConfig {
	private final String SAMPLE_DATA = "classpath:sample_data_h2.sql";
	
	@Autowired
	private DataSource datasource;
	
	@PostConstruct
	public void loadIfInMemory() throws Exception {
		Resource resource = webApplicationContext.getResource(SAMPLE_DATA);
		ScriptUtils.executeSqlScript(datasource.getConnection(), resource);
	}
}
6reactions
philwebbcommented, May 1, 2017

Currently the schema.sql file is taken to bean that the entire database definition is being created by that file. It would be nice if we could change DataSourceInitializer so that the schema.sql file doesn’t immediately trigger initialization. The ideal order is:

With hibernate:

  • Run schema.sql
  • Initialize Hibernate and allow it to create entity tables
  • Run data.sql

Without hibernate:

  • Run schema.sql
  • Run data.sql

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.sql or trying flyway for data population.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot running data.sql before creating entities in ...
sql for this. It works fine, entities are created and then data. sql is run to insert data in the tables produced by...
Read more >
Quick Guide on Loading Initial Data with Spring Boot - Baeldung
A quick and practical example of using data.sql and schema.sql files in Spring Boot.
Read more >
86. Database Initialization - Spring
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard...
Read more >
Spring Boot - Loading Initial Data - JavaByDeveloper
In this guide, you will see configuration support for initializing data and load schema.sql, data.sql and sql scripts with custom names.
Read more >
Loading Initial Data with Spring Boot
When we run our application, Spring Boot will create an empty table for us, but will not populate it for the above defined...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found