Note : I have opened an issue on the Spring boot repository. Here's is the link
https://github.com/spring-projects/spring-boot/issues/9048
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.
How can I get
data.sqlworking with theschema.sqland entity classes at the same time ?
Here's sample code for the project.
Git link for functional maven project to reproduce the issue.
https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue.git
package com.test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestDataSqlApplication {
public static void main(String[] args) {
SpringApplication.run(TestDataSqlApplication.class, args);
}
}
@Entity
@Table(name="USER_DETAILS")
class UserDetails {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
schema.sql
create table test(id int(10), name varchar(10));
data.sql
insert into USER_DETAILS VALUES(1, 'user1');
insert into USER_DETAILS VALUES(2, 'user2');
insert into USER_DETAILS VALUES(3, 'user3');
Edit *
As suggested by @abaghel renaming data.sql to import.sql works, however import.sql runs unconditionally. That's not what I need.
For the testing, I have a maven profile which activates a specific spring.datasource.platform = h2, which in turn forces spring to load schema-h2.sql and data-h2.sql. Unfortunately platform has no effect on the import.sql so renaming it to import-h2.sql stops Spring from loading it.
Here's the branch with the platform changes to reproduce this issue.
https://github.com/ConsciousObserver/SpringBootSchemaSqlIssue/tree/platform-h2
import.sqlisn't run by spring it is detected by hibernate and loaded as such. So it doesn't adhere to the rules that Spring Boot has for thedata.sqlandschema.sql. Also combining different ways of managing your database is probably not a good thing (imho) and can be confusing. I would suggest disabling hibernate from creating the schema and you put all the schema creation logic in theschema.sql.]data.sqlbefore entities are created only in presence ofschema.sqlwhen all it does is cause issues. Project requires content from some existing tables, which I'm creating in theschema-h2.sqlonly for testing. New tables are to be generated by entities, so I can't get away with using just the one.schema.sqlimho you shouldn't rely on hibernate creating the schema (at least not in your production environment you want more control over that). Combining theschema.sql,data.sqltogether with hibernate is tricky.data.sql.