how to perform create table and insert row at same time in application.properties file

Viewed 109

Either I can create table using spring.jpa.hibernate.ddl-auto=update

or I can insert into table using spring.datasource.initialization-mode=always

How to perform both operation at same time ?

If I do both operation it gives me an exeption as below

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptDataSourceInitializer' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceInitializationConfiguration$SharedCredentialsDataSourceInitializationConfiguration.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/JAY%20GURUDEV/eclipse-workspace/bootjpa/target/classes/data.sql]: insert into alien(aid,aname,tech) values(1,'ritu','java'); nested exception is java.sql.SQLSyntaxErrorException: Table 'mydb.alien' doesn't exist at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.14.jar:5.3.14]

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/JAY%20GURUDEV/eclipse-workspace/bootjpa/target/classes/data.sql]: insert into alien(aid,aname,tech) values(1,'ritu','java'); nested exception is java.sql.SQLSyntaxErrorException: Table 'mydb.alien' doesn't exist at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-5.3.14.jar:5.3.14] at Caused by: java.sql.SQLSyntaxErrorException: Table 'mydb.alien' doesn't exist at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.27.jar:8.0.27] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar:8.0.27]

I think this exception occurs because table is not created and row is trying to be inserted from data.sql file

0 Answers
Related