[英]If using Spring Boot and hibernate, should I use data.sql or import.sql to import data into my database, and why?
[英]Spring Boot dont read data.sql but it read import.sql
我想在 spring 創建表后導入 DML 腳本。 我正在處理data.sql
文件,但我的應用程序沒有看到它。 我不知道為什么。 當我將data.sql
重命名為import.sql
時它可以工作,但它也應該與data.sql
工作。
有人知道為什么嗎?
我的 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
我把data.sql
放入src/main/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'
當只有 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)'
當我在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)
我可以同時看到data.sql
和import.sql
在target/classes/data.sql
, target/classes/import.sql
...
如果將 data.sql 放在 jar 中,則在其名稱前加上類路徑或 META-INF
spring.datasource.data=classpath:/data.sql
spring.datasource.data=/META-INF/data.sql
(我不是 100% 確定,所以如果您嘗試兩種解決方案並給我反饋會很棒)
您必須通過注釋您的行 spring.jpa.hibernate.ddl-auto=create-drop 並將其設置為驗證來停用 Hibernate 加載(來自 import.sql)。 然后添加 spring.datasource.initialization:
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=validate
如果有人遇到這個問題,這就是我所做的:
應用程序屬性:
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>
幫助我的行是添加最后 2 行,還請記住,您必須有一個分別名為“data-oracle.sql”或“data-mysql.sql”的文件。
當 naXa 的第一個選項時為我工作:
spring.datasource.data=classpath:/data.sql
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.