Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot is not creating tables automatically

I'm facing an error when Spring Boot try to insert data in a table. I'm using H2 and Spring Boot 2.5.0. It's seems that Spring is not creating the table.

Here is my entity

@Entity(name = "Users")
@Table(name = "users")
public class UserEntity implements Serializable {

    // Serial

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    private String surname;

    private String email;

    // Getters and setters

}

And my repository

public interface UserRepository extends JpaRepository<UserEntity, Integer> {

}

I have added to my pom.xml h2 and spring-boot-starter-data-jpa.

When I start the server in debug mode I got this 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:/PATH/target/classes/data.sql]: INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', '[email protected]'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabla "USERS" no encontrada
Table "USERS" not found; SQL statement:
INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', '[email protected]') [42102-200]

This is my data.sql

INSERT INTO users (id, name, surname, email) VALUES (1, 'Mitchell', 'Hudson', '[email protected]');
INSERT INTO users (id, name, surname, email) VALUES (2, 'Melanie', 'Bell', '[email protected]');
INSERT INTO users (id, name, surname, email) VALUES (3, 'Diane', 'Ruiz', '[email protected]');

I try some configuration in properties file but I wasn't able to fix the error.

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.driver-class-name=org.h2.Driver

I can see the query to insert data but it's not generating query to create the table so I think that the problem is there.

Thanks in advance.

like image 381
cjgmj Avatar asked May 24 '21 20:05

cjgmj


1 Answers

The SQL Script DataSource Initialization feature has been redesigned in Spring Boot 2.5.

By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

Reference

Spring Boot 2.5 Release Notes

like image 50
eHayik Avatar answered Oct 20 '22 16:10

eHayik