Spring Boot中有多个SQL导入文件 [英] Multiple SQL import files in Spring Boot
问题描述
通过 Spring Boot参考手册,我们可以通过几种方式在启动应用程序时导入数据。结合内存数据库,这对于测试非常方便。
这些选项用于创建一个名为 import.sql
,它将被Hibernate选中,或者创建一个名为 data.sql
的文件,这个文件将被Spring JDBC使用。这两种方法对我来说都很好。
然而,我喜欢分开我的项目,所以我目前有一个核心域模型,其中有一些方便的导入配置核心数据,例如某些用户,这在任何地方都可以使用。我也有特定于函数的项目,重用基础数据的相同导入是有用的,但也会导入一些特定于该函数的附加数据。
这是事情不太好的地方。
我找到了答案前面的问题,Pascal Thivent提到,从Hibernate 3.6.0开始, hibernate.hbm2ddl.import_files
属性可用于定义文件列表。 Beta1的。鉴于我的项目正在导入4.3.1.Final,我认为这可能是可用的。
因此,我尝试将以下内容添加到我的Spring Boot application.properties
:
spring.jpa.hibernate.hbm2ddl.import_files = / another- import.sql
和:
hibernate.hbm2ddl.import_files = / another-import.sql
不幸的是,这些都不会导致导入运行。
所以我想知道我是否弄乱了上面的属性(很可能)。或者是有什么我需要做的?
请注意,作为一种解决方法,我发现Spring JDBC似乎运行 data.sql
在Hibernate运行 import.sql
之后。因此,只要我不需要两次以上的导入,我就可以使用 import.sql
作为基础数据,然后将项目特定的导入放在 data.sql
。我可以解决这个问题,但这不是一个真正的解决方案。
如果您真的想使用hibernate属性前缀使用 spring.jpa.properties。
作为 EntityManagerFactory
的属性。请参阅这里在Spring Boot参考指南中。
spring.jpa.properties.hibernate.hbm2ddl.import_files = file1 .sql,file2.sql
然而,您也可以使用 spring.datasource .data
和 spring.datasource.schema
属性。它们分别默认为 data
和 schema
。正如你可以在 DataSourceInitializer 类。你也可以设置它们,它们以逗号分隔的资源列表。
spring.datasource.data = classpath:/data-domain.sql,file:/ c:/ sql / data-reference .sql,data-complex.sql
它变得更好,因为资源加载也允许加载资源 ant风格模式。
spring.datasource.data = / META-INF / sql / init - *。sql
spring.datasource.schema = / META-INF / sql / schema - *。sql
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.
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
这篇关于Spring Boot中有多个SQL导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!