I have this project that I just started and using a model from my teacher I created a simple class to be mapped in the H2. So far no problems I run the application and the table is generated and I try some insert commands and they are fine but when I add the data.sql at the resources folder the project refuses to generate the schema.
Here is my application.properties file:
# DATABASE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.username=sa
spring.datasource.password=
# JPA
spring.jpa.hibernate.ddl-auto=update
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
The SuperHero.class:
@Entity
@Table(name = "super_hero")
@Data
public class SuperHero {
@Id
@Column(name="id", updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false, columnDefinition = "TEXT")
private String name;
}
The data.sql file:
INSERT INTO super_hero(id, name) VALUES (1, 'Super Man');
This is the error:
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/gabri/git/plexus-super-heroes/target/classes/data.sql]: INSERT
INTO SUPER_HERO(id, name) VALUES (1, 'Super Man'); nested exception is
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "SUPER_HERO" not found; SQL statement:
INSERT INTO SUPER_HERO(id, name) VALUES (1, 'Super Man') [42102-200]
I tried to change to lowercase and upercase the table name, recreate the project from the scratch but keep getting this error.
You can put your statements in import.sql instead of data.sql and retry. Hibernate uses this file to initialize the table.