Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SQL import files in Spring Boot

Going by the Spring Boot reference manual, there are a couple of ways in which we can import data on startup of an application. Combined with an in-memory database, this is rather handy for testing.

The options are to create a file called import.sql, which will be picked up by Hibernate, or to create a file called data.sql, which will be picked up by Spring JDBC. Both of these work fine for me.

However, I like to break up my projects a bit, so I currently have a core domain model, where there are some handy imports to configure core data such as some users, which is used everywhere. I also have function-specific projects where it is useful to re-use that same import of base data, but also import some additional data which is specific to that function.

This is where things are not working so well.

I found an answer to a previous question, where Pascal Thivent mentioned that the hibernate.hbm2ddl.import_files property could be used to define a list of files, as of Hibernate 3.6.0.Beta1. Given that my project is importing 4.3.1.Final, I thought that perhaps this would be available.

So I tried adding the following to my Spring Boot application.properties:

spring.jpa.hibernate.hbm2ddl.import_files=/another-import.sql 

and:

hibernate.hbm2ddl.import_files=/another-import.sql 

Unfortunately, neither of these would cause the import to run.

So I'm wondering whether I just made a mess of the properties above (quite likely). Or is there something else that I need to do?

Note that as a workaround, I spotted that Spring JDBC seems to run data.sql after Hibernate runs import.sql. So as long as I don't need more than two imports, I'm able to use import.sql for the base data and then put project-specific imports in data.sql. I can get by with this, but it's not really a solution.

like image 665
Steve Avatar asked Jul 01 '14 10:07

Steve


People also ask

Where do I put sql data in spring boot?

We can define schema by creating a SQL file in the resource folder (src/main/resource). We can populate data in the table by creating a SQL file in the resource folder (src/main/resource). Spring Boot automatically picks up the data. sql file and run it against the H2 database during the application startup.

What is DDL Auto in spring boot?

ddl-auto in Spring Boot configuration file to create or create-drop . If you set ddl-auto to create or create-drop, Hibernate will generate a schema for your entity based on its mapping. You need to add the following property in your application. properties file. spring.jpa.hibernate.ddl-auto=create.

What is sql init mode always?

This property takes one of three values: always – always initialize the database. embedded – always initialize if an embedded database is in use. This is the default if the property value is not specified. never – never initialize the database.

What is spring JPA generate DDL?

JPA has features for DDL generation, and these can be set up to run on startup against the database. This is controlled through two external properties: spring. jpa. generate-ddl (boolean) switches the feature on and off and is vendor independent.


1 Answers

If you really want to use the hibernate property prefix it with spring.jpa.properties. as those are added as is as properties to the EntityManagerFactory. See here in the Spring Boot reference guide.

spring.jpa.properties.hibernate.hbm2ddl.import_files=file1.sql,file2.sql 

However you can also use the spring.datasource.data and spring.datasource.schema properties to your advantage. They default to respectively data and schema. As you can see in the DataSourceInitializer class. You can also set them and they take a comma separated list of resources.

spring.datasource.data=classpath:/data-domain.sql,file:/c:/sql/data-reference.sql,data-complex.sql 

It gets even better because the resource loading also allows loading resources with ant-style patterns.

spring.datasource.data=/META-INF/sql/init-*.sql spring.datasource.schema=/META-INF/sql/schema-*.sql  
like image 120
M. Deinum Avatar answered Oct 08 '22 22:10

M. Deinum