Spring Boot is not creating tables automatically
Asked Answered
D

3

8

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.

Delft answered 24/5, 2021 at 20:3 Comment(2)
have you defined a repository interface that extends JPARepository?Fatality
@JAsgarov yes, I add it to the main post, I have other project with the same configuration but using Spring Boot 2.3.0.RELEASE and it's working properly. I think that something change in the way of configure the project and im missing it.Delft
S
32

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 - Hibernate and data.sql

Secession answered 24/5, 2021 at 21:2 Comment(3)
I found it a few minutes ago and I came to comment it but you was faster. That's solve the problem! I found it here.Delft
I am thankful for this (original question) and this answer to highlight the issue. I am worn out from "been redesigned" spring data black magic voodoo changes. 10.9.2. Initialize a Database Using Hibernate You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded. docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… "it thinks"Onondaga
@Secession Thank you very much. I struggled for 2 days to resolve this issue. lolGeisel
F
0

If you are working on a maven project, spring.jpa.defer-datasource-initialization=true should be added in both src/main/resources/application.properties(for Spring Boot Application to run properly) and src/test/resources/application.properties (for mvn clean install to execute successfully).

PS: I am using Spring boot 3.0.2

Frankish answered 15/2, 2023 at 4:42 Comment(0)
S
0

Make sure adding dependence of h2 database here both dependence for gradle

enter code here
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'

maven dependence

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=create-drop change to update it mean when rerun application or project your previous data in database will not lose.

Sanderlin answered 15/2, 2023 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.