2
respostas

Error creating bean with name 'dataSourceScriptDatabaseInitializer' - Table "CURSO" not found

Olá, toda vez que tento executar o projeto aparece o erro abaixo:

 :: Spring Boot ::                (v2.6.3)

022-02-22 00:44:49.217  INFO 11984 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-22 00:44:49.369 ERROR 11984 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

**org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/Public/forum/target/classes/data.sql]: insert into curso(id, nome, categoria) values(1, 'Kotlin', 'Programacao'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CURSO" not found; SQL statement:
insert into curso(id, nome, categoria) values(1, 'Kotlin', 'Programacao') [42102-200]**
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.3.jar:2.6.3]
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/Public/forum/target/classes/data.sql]: insert into curso(id, nome, categoria) values(1, 'Kotlin', 'Programacao'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CURSO" not found; SQL statement:
insert into curso(id, nome, categoria) values(1, 'Kotlin', 'Programacao') [42102-200]

Segue meu arquivo application.yml

spring:
  datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:forum
    username: sa
    password:
      jpa:
        spring.jpa.defer-datasource-initialization=true:
        database-platform: org.hibernate.dialect.H2Dialect
        properties:
          hibernate:
            show_sql: true
            format_sql: true

Pesquisei no fórum porém não encontrei uma solução para o problema.

2 respostas

Oi Richard,

Tem uma propriedade sua que está incorreta: spring.jpa.defer-datasource-initialization

Altere seu arquivo yml para ficar assim:

spring:
  datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:forum
    username: sa
    password:
      jpa:
        defer-datasource-initialization: true
        database-platform: org.hibernate.dialect.H2Dialect
        properties:
          hibernate:
            show_sql: true
            format_sql: true

Opa Rodrigo, obrigado pela pronta resposta, eu resolvi copiando o arquivo Pom.XML ele baixou algumas dependências que estavam faltando e funcionou. Mas o arquivo application.yml estava incorreto tbm copiei do que estava no github e deu certo. Abraços!