[英]Spring Boot dont read data.sql but it read import.sql
I want import DML script after spring creates tables.我想在 spring 创建表后导入 DML 脚本。 I was fighting with data.sql
file but my application don't see it .我正在处理data.sql
文件,但我的应用程序没有看到它。 I don't know why.我不知道为什么。 It works when I rename data.sql
to import.sql
, but it should also work with data.sql
.当我将data.sql
重命名为import.sql
时它可以工作,但它也应该与data.sql
工作。
Anybody know why?有人知道为什么吗?
My application.properties:我的 application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/yyy
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=org.postgresql.Driver
I put data.sql
into src/main/resources
我把data.sql
放入src/main/resources
When only data.sql is in resources
:当只有 data.sql 在resources
:
2018-03-21 00:42:13.646 INFO 4740 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@eebc0db'
When only import.sql (also in src/main/resources
):当只有 import.sql (也在src/main/resources
)时:
2018-03-21 00:48:57.023 INFO 16600 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'ScriptSourceInputFromUrl(file:/C:/Users/Pawel/Desktop/Project/target/classes/import.sql)'
When i type spring.datasource.data=data.sql
into application.properties
当我在application.properties
输入spring.datasource.data=data.sql
Exception in thread "SimpleAsyncTaskExecutor-2" org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.data with value 'ServletContext resource [/data.sql]' is invalid: The specified resource does not exist.
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:169)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:151)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:114)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:93)
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:37)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.lambda$multicastEvent$0(SimpleApplicationEventMulticaster.java:136)
at java.lang.Thread.run(Thread.java:745)
I can see both data.sql
and import.sql
in target/classes/data.sql
, target/classes/import.sql
...我可以同时看到data.sql
和import.sql
在target/classes/data.sql
, target/classes/import.sql
...
If you put data.sql inside jar then prepend its name with classpath or META-INF如果将 data.sql 放在 jar 中,则在其名称前加上类路径或 META-INF
spring.datasource.data=classpath:/data.sql
spring.datasource.data=/META-INF/data.sql
(I'm not 100% sure so it would be great if you try both solution and give me feedback) (我不是 100% 确定,所以如果您尝试两种解决方案并给我反馈会很棒)
you have to inactivate the Hibernate loading (from import.sql) by commenting your line spring.jpa.hibernate.ddl-auto=create-drop and setting it to validate.您必须通过注释您的行 spring.jpa.hibernate.ddl-auto=create-drop 并将其设置为验证来停用 Hibernate 加载(来自 import.sql)。 Then add spring.datasource.initialization:然后添加 spring.datasource.initialization:
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=validate
If somebody have this trouble, here is what I did:如果有人遇到这个问题,这就是我所做的:
applications.properties :应用程序属性:
spring.datasource.url = jdbc:oracle:thin:@localhost:1521:xe<br>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver<br>
spring.datasource.username=xxxx<br>
spring.datasource.password=xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=oracle<br>
spring.datasource.url = jdbc:mysql://localhost:3306/mysql<br>
spring.datasource.username = xxxx<br>
spring.datasource.password = xxxx<br>
spring.jpa.hibernate.ddl-auto=none<br>
spring.datasource.initialization-mode=always<br>
spring.datasource.platform=mysql<br>
The lines that helped me was adding the last 2, and also remember that you must have a file with the name "data-oracle.sql" or "data-mysql.sql" respectively.帮助我的行是添加最后 2 行,还请记住,您必须有一个分别名为“data-oracle.sql”或“data-mysql.sql”的文件。
Here is the source:这是来源:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
work for me when first option of naXa:当 naXa 的第一个选项时为我工作:
spring.datasource.data=classpath:/data.sql spring.datasource.data=classpath:/data.sql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.