Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

hibernate - 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.

question from:https://stackoverflow.com/questions/24508223/multiple-sql-import-files-in-spring-boot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...