I have a little problem. I have a Spring Boot Application and I will fill my H2 database with data. But I can not load the initial database data from the data-h2.sql file.
Model:
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "mood")
public class Mood {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "mood_id")
private int id;
@Column(name = "name")
private String name;
public Mood(String name) {
this.name = name;
}
public Mood(int id, String name) {
this.id = id;
this.name = name;
}
}
data-h2.sql file:
INSERT INTO mood (name) VALUES ('Good');
application.properties file:
spring.datasource.url=jdbc:h2:mem:mooddb;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=a
spring.datasource.password=a
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.defer-datasource-initialization=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
The problem is that Spring Boot expects to find a file with either the name schema.sql or data.sql in the classpath to load.
But your file is named data-h2.sql so spring will not consider it, even if it is correctly placed in resources location (i.e either src/main/resources/ or src/test/resources).
It could search for a file with the name data-h2.sql and load it if you had configured the property spring.datasource.platform = h2.
Without using the aforementioned property, there are 2 solutions:
Rename the file into data.sql and be sure it is inside the resources folder
Do not rename the file and use the following application property to inform spring about the name of the file that it should load spring.sql.init.data-locations=classpath:data-h2.sql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With